How to set a global property on the string constructor in NodeJS? [duplicate] - node.js

This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 2 years ago.
I want to define a property on the string constructor, and I want then to access this property from any file in my project. for example:
file1.js
String.capitalize = function(){
this.//...etc
}
file2.js
"Hello nice to meet you".capitalize()

You can define a new String prototype function like in the example below
String.prototype.capitalize = function(){
return this.toUpperCase();
};
console.log("Hello nice to meet you".capitalize());

Related

pass a string by a value in function call [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I would like to pass a string instead of the number. But don't know how to do that.
#define MODULE_A 5
#define MODULE_B 5
void function_2(unsigned moduleId, IdData)
{
if ( MODULE_A == moduleId) // want to compare the string instead of number 5 since both A and B are same.
{
// do something
}
if (MODULE_B == moduleId) //want to compare the string instead of number 5 since both A and B are same.
{
//do something
}
}
void function_1()
{
function_2(MODULE_B, IdData);
}
void function_3()
{
function_1(MODULE_A, IdData);
}
Welcome to do any workaround to archive the expected result. Basically, I have a function_2 that has the 1st element as unsigned integer, But when other function calling, I would like to pass the string inside each if condition but rather the value, since both module IDs are macro defined as the same number. Thank you.

How to collect user defined functions in a liberary in nodjs? [duplicate]

This question already has answers here:
In Node.js, how do I "include" functions from my other files?
(28 answers)
Best way to structure helpers functions in NodeJS
(2 answers)
Closed 5 years ago.
I have several functions but I dont know how collect user defined functions in liberary to reuse them in all around the nodeJs projec?
you could do something like this:
In your preferred folder, create a file like user.js
Then, define a class, E6 way :
class Users {
//if you need a constructor, but it's not mandatory
constructor(username,email,otherParamYouNeed){
this.username = username;
this.email = email;
this.otherParamYouNeed = otherYouNeed
}
//then organize your user functions there
userFunctionThatDoesSomething(){
//do what you need
}
userFunctionThatDoesAnotherThing(){
// do what you need
}
}
//then export the class
module.exports = {Users}
After, in the file in which you need to call those functions :
var {Users} = require ('/path/to/user.js');
//if you have constructor in your class
var user = new Users(username,email,otherParamYouNeed);
//if not
var user = new Users;
After that, you will be able to use the functions you declared in the class in the file you have required the class in, like :
user.userFunctionThatDoesSomething(etc..);
Have a look at https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/

How do I call concat_idents inside a recursive macro expansion? [duplicate]

This question already has answers here:
Can a Rust macro create new identifiers?
(4 answers)
Closed 5 years ago.
I'm trying to create a macro to expand a set of names and define fields for a struct:
macro_rules! expand {
($($name:ident),*) => {
pub struct Test {
$(
concat_idents!(var_, $name) : Vec<$name>
),*
}
}
}
//fails
expand!(a,b,c);
This fails as the compiler does not recognize concat_idents! as a macro. How am I supposed to work around this?
It is not at present possible to do this in any way. Substitutions ($foo) are fine there, but not macro calls.

How to call a method from another class in groovy? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
For Instance, I have a class like this:
class firstOne{
....
def A (){
}
}
class secondOne{
// I need to call and use method A from class firstOne
// even I get error if I try to follow Java like calls
// firstOne method = new firstOne();
// method.A()
}
I already tried http://groovy.codehaus.org/Scripts+and+Classes and http://groovy.codehaus.org/Groovy+Beans but no way. Any kind of suggestion or examples would be really helpful.
I don't see any problem in this:
class FirstOne {
def a() {
println "a"
}
}
class SecondOne {
def b() {
new FirstOne().a()
println "b"
}
}
new FirstOne().a()
println("")
new SecondOne().b()
Output:
a
a
b
This is not specific to Groovy/Grails:
firstOne first = new firstOne()
first.A()
Also you should capitalize the first letter of classes, but not methods (as is best practice in Java).

Get the name of the current invoking method at runtime [duplicate]

This question already has answers here:
Groovy, get enclosing function's name?
(5 answers)
Closed 8 years ago.
Is it possible to get the name of the current method in Groovy code?
def myMethod() {
// want to get the string myMethod here
}
Thanks
Tim_yates gives you a link to general solution.
This is shorter way to get method name. May be optimized, leawed as is for the sake of clarity.
class A {
def myMethod() {
for (m in new Throwable().stackTrace) {
if (m.className == this.class.name)
return m.methodName
}
}
}
println new A().myMethod()

Resources