Add a property to a node file Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWhat is the 'global' object in NodeJSDetecting an undefined object propertyHow to efficiently count the number of keys/properties of an object in JavaScript?How can I upload files asynchronously?Add table row in jQueryHow can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I include a JavaScript file in another JavaScript file?Sort array of objects by string property valueIterate through object propertiesWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?
What to do with post with dry rot?
Choo-choo! Word trains
Why does tar appear to skip file contents when output file is /dev/null?
How to say that you spent the night with someone, you were only sleeping and nothing else?
Stop battery usage [Ubuntu 18]
Simulating Exploding Dice
Jazz greats knew nothing of modes. Why are they used to improvise on standards?
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
Blender game recording at the wrong time
I'm thinking of a number
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
How does modal jazz use chord progressions?
Is 1 ppb equal to 1 μg/kg?
Limit for e and 1/e
How is simplicity better than precision and clarity in prose?
What LEGO pieces have "real-world" functionality?
How to rotate it perfectly?
Why is "Captain Marvel" translated as male in Portugal?
What would be Julian Assange's expected punishment, on the current English criminal law?
Do working physicists consider Newtonian mechanics to be "falsified"?
Fishing simulator
Complexity of many constant time steps with occasional logarithmic steps
Active filter with series inductor and resistor - do these exist?
If A makes B more likely then B makes A more likely"
Add a property to a node file
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhat is the 'global' object in NodeJSDetecting an undefined object propertyHow to efficiently count the number of keys/properties of an object in JavaScript?How can I upload files asynchronously?Add table row in jQueryHow can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I include a JavaScript file in another JavaScript file?Sort array of objects by string property valueIterate through object propertiesWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Is it possible to add a property (with get
and set
method) to the scope of a file without making it global? (Similar to how let
or const
would work for a variable declaration)
This is the code I've written so far, It can add a property to the global scope.
var propertyValue;
Object.defineProperty(global, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(PropertyValue);
Is it possible to make the property only visible to just the file it was declared in. The same thing can be done by declaring a variable and adding all properties there.
var fileProperties;
var propertyValue;
Object.defineProperty(fileProperties, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(fileProperties.PropertyValue);
But then I still need to type the name of that variable every time I want to get/set a property.
So is there a way to create a property that
- Is not fully global
- Can be accessed without stating the owner object
- Can be recognized by eslint
javascript node.js properties this global
add a comment |
Is it possible to add a property (with get
and set
method) to the scope of a file without making it global? (Similar to how let
or const
would work for a variable declaration)
This is the code I've written so far, It can add a property to the global scope.
var propertyValue;
Object.defineProperty(global, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(PropertyValue);
Is it possible to make the property only visible to just the file it was declared in. The same thing can be done by declaring a variable and adding all properties there.
var fileProperties;
var propertyValue;
Object.defineProperty(fileProperties, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(fileProperties.PropertyValue);
But then I still need to type the name of that variable every time I want to get/set a property.
So is there a way to create a property that
- Is not fully global
- Can be accessed without stating the owner object
- Can be recognized by eslint
javascript node.js properties this global
add a comment |
Is it possible to add a property (with get
and set
method) to the scope of a file without making it global? (Similar to how let
or const
would work for a variable declaration)
This is the code I've written so far, It can add a property to the global scope.
var propertyValue;
Object.defineProperty(global, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(PropertyValue);
Is it possible to make the property only visible to just the file it was declared in. The same thing can be done by declaring a variable and adding all properties there.
var fileProperties;
var propertyValue;
Object.defineProperty(fileProperties, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(fileProperties.PropertyValue);
But then I still need to type the name of that variable every time I want to get/set a property.
So is there a way to create a property that
- Is not fully global
- Can be accessed without stating the owner object
- Can be recognized by eslint
javascript node.js properties this global
Is it possible to add a property (with get
and set
method) to the scope of a file without making it global? (Similar to how let
or const
would work for a variable declaration)
This is the code I've written so far, It can add a property to the global scope.
var propertyValue;
Object.defineProperty(global, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(PropertyValue);
Is it possible to make the property only visible to just the file it was declared in. The same thing can be done by declaring a variable and adding all properties there.
var fileProperties;
var propertyValue;
Object.defineProperty(fileProperties, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(fileProperties.PropertyValue);
But then I still need to type the name of that variable every time I want to get/set a property.
So is there a way to create a property that
- Is not fully global
- Can be accessed without stating the owner object
- Can be recognized by eslint
javascript node.js properties this global
javascript node.js properties this global
edited Mar 23 at 12:32
nick zoum
asked Jun 17 '18 at 18:36
nick zoumnick zoum
2,72411542
2,72411542
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
A property should be accessed on some object, the only possibilities for an object to be omitted in JavaScript are global properties and with
statement.
As the original code shows, this will access a property on global variable, using global variables for local tasks is a bad practice, Using global variables for local tasks is a bad practice:
Object.defineProperty(global, "PropertyValue", ...);
console.log(PropertyValue);
Another way is to use with
statement, which is deprecated and won't work in strict mode:
Object.defineProperty(someObject, "PropertyValue", ...);
with (someObject)
console.log(PropertyValue);
In Node, a script is evaluated in the scope of module wrapper function, this.PropertyValue
refers to module.exports.PropertyValue
in module scope.
A property can be defined on export object explicitly:
let propertyValue;
Object.defineProperty(exports, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(exports.PropertyValue);
PropertyValue
will be available to other modules when this module is imported. There are usually no good reasons to enforce encapsulation to the point where it starts to make developer's life harder. If PropertyValue
isn't intended to be used outside the module, usually it's enough to use Hungarian notation and underscore internal/private property:
Object.defineProperty(exports, "_PropertyValue", ... );
This way it's still available for testing.
While this explain howthis
refers to theexports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)
– nick zoum
Mar 23 at 12:33
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to useexports
is that it already exists.
– estus
Mar 23 at 13:03
add a comment |
If you want scope of variable limited to that file only use this
instead of global
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
You can access it directly when usingglobal
instead ofthis
and you can do the same withwindow
andthis
in a browser.
– nick zoum
Mar 22 at 8:54
Accessing a variable fromthis
usually confuses most intellisenses even if you have declared it usingjsdoc
– nick zoum
Mar 22 at 9:03
1
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
add a comment |
The answer all your questions:
Is not fully global.
Can be accessed without stating the owner object.
Can be recognized by eslint.
is to use const
or let
statements.
From the documentation of const
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
From the documentation of let
The let statement declares a block scope local variable, optionally initializing it to a value.
Since you have a setter method, I would recommend usinglet
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.
– Mak
Mar 22 at 1:20
1
How do you define a property (withget
andset
functions) usinglet
/const
?
– nick zoum
Mar 22 at 7:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50899435%2fadd-a-property-to-a-node-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A property should be accessed on some object, the only possibilities for an object to be omitted in JavaScript are global properties and with
statement.
As the original code shows, this will access a property on global variable, using global variables for local tasks is a bad practice, Using global variables for local tasks is a bad practice:
Object.defineProperty(global, "PropertyValue", ...);
console.log(PropertyValue);
Another way is to use with
statement, which is deprecated and won't work in strict mode:
Object.defineProperty(someObject, "PropertyValue", ...);
with (someObject)
console.log(PropertyValue);
In Node, a script is evaluated in the scope of module wrapper function, this.PropertyValue
refers to module.exports.PropertyValue
in module scope.
A property can be defined on export object explicitly:
let propertyValue;
Object.defineProperty(exports, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(exports.PropertyValue);
PropertyValue
will be available to other modules when this module is imported. There are usually no good reasons to enforce encapsulation to the point where it starts to make developer's life harder. If PropertyValue
isn't intended to be used outside the module, usually it's enough to use Hungarian notation and underscore internal/private property:
Object.defineProperty(exports, "_PropertyValue", ... );
This way it's still available for testing.
While this explain howthis
refers to theexports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)
– nick zoum
Mar 23 at 12:33
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to useexports
is that it already exists.
– estus
Mar 23 at 13:03
add a comment |
A property should be accessed on some object, the only possibilities for an object to be omitted in JavaScript are global properties and with
statement.
As the original code shows, this will access a property on global variable, using global variables for local tasks is a bad practice, Using global variables for local tasks is a bad practice:
Object.defineProperty(global, "PropertyValue", ...);
console.log(PropertyValue);
Another way is to use with
statement, which is deprecated and won't work in strict mode:
Object.defineProperty(someObject, "PropertyValue", ...);
with (someObject)
console.log(PropertyValue);
In Node, a script is evaluated in the scope of module wrapper function, this.PropertyValue
refers to module.exports.PropertyValue
in module scope.
A property can be defined on export object explicitly:
let propertyValue;
Object.defineProperty(exports, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(exports.PropertyValue);
PropertyValue
will be available to other modules when this module is imported. There are usually no good reasons to enforce encapsulation to the point where it starts to make developer's life harder. If PropertyValue
isn't intended to be used outside the module, usually it's enough to use Hungarian notation and underscore internal/private property:
Object.defineProperty(exports, "_PropertyValue", ... );
This way it's still available for testing.
While this explain howthis
refers to theexports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)
– nick zoum
Mar 23 at 12:33
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to useexports
is that it already exists.
– estus
Mar 23 at 13:03
add a comment |
A property should be accessed on some object, the only possibilities for an object to be omitted in JavaScript are global properties and with
statement.
As the original code shows, this will access a property on global variable, using global variables for local tasks is a bad practice, Using global variables for local tasks is a bad practice:
Object.defineProperty(global, "PropertyValue", ...);
console.log(PropertyValue);
Another way is to use with
statement, which is deprecated and won't work in strict mode:
Object.defineProperty(someObject, "PropertyValue", ...);
with (someObject)
console.log(PropertyValue);
In Node, a script is evaluated in the scope of module wrapper function, this.PropertyValue
refers to module.exports.PropertyValue
in module scope.
A property can be defined on export object explicitly:
let propertyValue;
Object.defineProperty(exports, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(exports.PropertyValue);
PropertyValue
will be available to other modules when this module is imported. There are usually no good reasons to enforce encapsulation to the point where it starts to make developer's life harder. If PropertyValue
isn't intended to be used outside the module, usually it's enough to use Hungarian notation and underscore internal/private property:
Object.defineProperty(exports, "_PropertyValue", ... );
This way it's still available for testing.
A property should be accessed on some object, the only possibilities for an object to be omitted in JavaScript are global properties and with
statement.
As the original code shows, this will access a property on global variable, using global variables for local tasks is a bad practice, Using global variables for local tasks is a bad practice:
Object.defineProperty(global, "PropertyValue", ...);
console.log(PropertyValue);
Another way is to use with
statement, which is deprecated and won't work in strict mode:
Object.defineProperty(someObject, "PropertyValue", ...);
with (someObject)
console.log(PropertyValue);
In Node, a script is evaluated in the scope of module wrapper function, this.PropertyValue
refers to module.exports.PropertyValue
in module scope.
A property can be defined on export object explicitly:
let propertyValue;
Object.defineProperty(exports, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(exports.PropertyValue);
PropertyValue
will be available to other modules when this module is imported. There are usually no good reasons to enforce encapsulation to the point where it starts to make developer's life harder. If PropertyValue
isn't intended to be used outside the module, usually it's enough to use Hungarian notation and underscore internal/private property:
Object.defineProperty(exports, "_PropertyValue", ... );
This way it's still available for testing.
edited Mar 23 at 13:00
answered Mar 22 at 19:53
estusestus
79.6k24116237
79.6k24116237
While this explain howthis
refers to theexports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)
– nick zoum
Mar 23 at 12:33
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to useexports
is that it already exists.
– estus
Mar 23 at 13:03
add a comment |
While this explain howthis
refers to theexports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)
– nick zoum
Mar 23 at 12:33
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to useexports
is that it already exists.
– estus
Mar 23 at 13:03
While this explain how
this
refers to the exports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)– nick zoum
Mar 23 at 12:33
While this explain how
this
refers to the exports
object when a containing object hasn't been defined. It still doesn't answer the question. Same thing can be achieved using a new variable and without making the variable public (updated question to include example)– nick zoum
Mar 23 at 12:33
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to use
exports
is that it already exists.– estus
Mar 23 at 13:03
Yes, the question didn't state the intention clearly. No, this isn't possible. I added the explanation. A reason to use
exports
is that it already exists.– estus
Mar 23 at 13:03
add a comment |
If you want scope of variable limited to that file only use this
instead of global
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
You can access it directly when usingglobal
instead ofthis
and you can do the same withwindow
andthis
in a browser.
– nick zoum
Mar 22 at 8:54
Accessing a variable fromthis
usually confuses most intellisenses even if you have declared it usingjsdoc
– nick zoum
Mar 22 at 9:03
1
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
add a comment |
If you want scope of variable limited to that file only use this
instead of global
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
You can access it directly when usingglobal
instead ofthis
and you can do the same withwindow
andthis
in a browser.
– nick zoum
Mar 22 at 8:54
Accessing a variable fromthis
usually confuses most intellisenses even if you have declared it usingjsdoc
– nick zoum
Mar 22 at 9:03
1
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
add a comment |
If you want scope of variable limited to that file only use this
instead of global
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
If you want scope of variable limited to that file only use this
instead of global
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
var propertyValue;
Object.defineProperty(this, "PropertyValue",
get: function ()
return propertyValue;
,
set: function (value)
propertyValue = value;
);
console.log(this.PropertyValue); // prints undefined
propertyValue=a:1
console.log(this.PropertyValue) // prints a:1
answered Mar 22 at 8:06
Vikash SinghVikash Singh
881520
881520
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
You can access it directly when usingglobal
instead ofthis
and you can do the same withwindow
andthis
in a browser.
– nick zoum
Mar 22 at 8:54
Accessing a variable fromthis
usually confuses most intellisenses even if you have declared it usingjsdoc
– nick zoum
Mar 22 at 9:03
1
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
add a comment |
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
You can access it directly when usingglobal
instead ofthis
and you can do the same withwindow
andthis
in a browser.
– nick zoum
Mar 22 at 8:54
Accessing a variable fromthis
usually confuses most intellisenses even if you have declared it usingjsdoc
– nick zoum
Mar 22 at 9:03
1
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
I've tried that, it can't be accessed without stating the owner object and it can't be recognized by eslint.
– nick zoum
Mar 22 at 8:16
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
well, you can't access without stating owner object because its the property of that object. In terms of es6 if you want to access some property of class you have to use class or instance of it. I think your 2nd point is hypothetical.
– Vikash Singh
Mar 22 at 8:37
You can access it directly when using
global
instead of this
and you can do the same with window
and this
in a browser.– nick zoum
Mar 22 at 8:54
You can access it directly when using
global
instead of this
and you can do the same with window
and this
in a browser.– nick zoum
Mar 22 at 8:54
Accessing a variable from
this
usually confuses most intellisenses even if you have declared it using jsdoc
– nick zoum
Mar 22 at 9:03
Accessing a variable from
this
usually confuses most intellisenses even if you have declared it using jsdoc
– nick zoum
Mar 22 at 9:03
1
1
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
global variable is shared across all the files in nodejs whereas variable declared within file has scope limited that file only. I have few sources : 1. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 2. stackabuse.com/using-global-variables-in-node-js 3. stackoverflow.com/questions/43627622/… 4. medium.com/quick-code/…
– Vikash Singh
Mar 25 at 5:59
add a comment |
The answer all your questions:
Is not fully global.
Can be accessed without stating the owner object.
Can be recognized by eslint.
is to use const
or let
statements.
From the documentation of const
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
From the documentation of let
The let statement declares a block scope local variable, optionally initializing it to a value.
Since you have a setter method, I would recommend usinglet
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.
– Mak
Mar 22 at 1:20
1
How do you define a property (withget
andset
functions) usinglet
/const
?
– nick zoum
Mar 22 at 7:40
add a comment |
The answer all your questions:
Is not fully global.
Can be accessed without stating the owner object.
Can be recognized by eslint.
is to use const
or let
statements.
From the documentation of const
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
From the documentation of let
The let statement declares a block scope local variable, optionally initializing it to a value.
Since you have a setter method, I would recommend usinglet
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.
– Mak
Mar 22 at 1:20
1
How do you define a property (withget
andset
functions) usinglet
/const
?
– nick zoum
Mar 22 at 7:40
add a comment |
The answer all your questions:
Is not fully global.
Can be accessed without stating the owner object.
Can be recognized by eslint.
is to use const
or let
statements.
From the documentation of const
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
From the documentation of let
The let statement declares a block scope local variable, optionally initializing it to a value.
The answer all your questions:
Is not fully global.
Can be accessed without stating the owner object.
Can be recognized by eslint.
is to use const
or let
statements.
From the documentation of const
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
From the documentation of let
The let statement declares a block scope local variable, optionally initializing it to a value.
answered Mar 21 at 23:00
WebRookieWebRookie
19713
19713
Since you have a setter method, I would recommend usinglet
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.
– Mak
Mar 22 at 1:20
1
How do you define a property (withget
andset
functions) usinglet
/const
?
– nick zoum
Mar 22 at 7:40
add a comment |
Since you have a setter method, I would recommend usinglet
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.
– Mak
Mar 22 at 1:20
1
How do you define a property (withget
andset
functions) usinglet
/const
?
– nick zoum
Mar 22 at 7:40
Since you have a setter method, I would recommend using
let
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.– Mak
Mar 22 at 1:20
Since you have a setter method, I would recommend using
let
depending the type of data you want to store. You could use a specific convention like all upper case letters in the name to denote a file property.– Mak
Mar 22 at 1:20
1
1
How do you define a property (with
get
and set
functions) using let
/const
?– nick zoum
Mar 22 at 7:40
How do you define a property (with
get
and set
functions) using let
/const
?– nick zoum
Mar 22 at 7:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50899435%2fadd-a-property-to-a-node-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown