Is there a way to add a variable to the scope in a similar way to how variables are added to the window...
Just out of curiosity:
In the context of a web browser, it is possible to add a property to the window object, consequently making it global, such as:
function a(){
window.b="c";
}
//edit
a();
console.log(b);
I was wondering if it is possible to set variables in a similar way, but only to the current scope, such as:
function a(){
scope.b="c";
console.log(b);
}
//but "b" is not available here.
Possible use case
var objectOne=new(function(){
this.a="e";
this.b="f";
this.c="g";
})();
(function(){
var importlist=["a","b"];
var scopehere=scope;
for(var importitm of importlist){
scopehere[importitm]=objectOne[importitm];
}
console.log(a,b);
})();
So in this case, it would be possible to "import" many vars serially, have them accessible within the object as vars instead of using "this", (like when you export them to window), but in this case they are private.
In particular, if you are using an html-loaded script and you'd like to make the script properties available in the scope. Examples: paper.js, three.js. Going property by property would be a bit cumbersome, but it could be done automatically if there was such a "scope" object.
I know that you can work around in many ways, and perhaps there are not such substantial benefits to doing this. This question is merely out of curiosity.
Changes to this post:
added a call to function ´a()´ as pointed out by kylestephens.
javascript scope
add a comment |
Just out of curiosity:
In the context of a web browser, it is possible to add a property to the window object, consequently making it global, such as:
function a(){
window.b="c";
}
//edit
a();
console.log(b);
I was wondering if it is possible to set variables in a similar way, but only to the current scope, such as:
function a(){
scope.b="c";
console.log(b);
}
//but "b" is not available here.
Possible use case
var objectOne=new(function(){
this.a="e";
this.b="f";
this.c="g";
})();
(function(){
var importlist=["a","b"];
var scopehere=scope;
for(var importitm of importlist){
scopehere[importitm]=objectOne[importitm];
}
console.log(a,b);
})();
So in this case, it would be possible to "import" many vars serially, have them accessible within the object as vars instead of using "this", (like when you export them to window), but in this case they are private.
In particular, if you are using an html-loaded script and you'd like to make the script properties available in the scope. Examples: paper.js, three.js. Going property by property would be a bit cumbersome, but it could be done automatically if there was such a "scope" object.
I know that you can work around in many ways, and perhaps there are not such substantial benefits to doing this. This question is merely out of curiosity.
Changes to this post:
added a call to function ´a()´ as pointed out by kylestephens.
javascript scope
JavaScript provides no way of explicitly accessing the local scope (other than direct variable references of course).
– Pointy
Nov 13 '18 at 16:07
add a comment |
Just out of curiosity:
In the context of a web browser, it is possible to add a property to the window object, consequently making it global, such as:
function a(){
window.b="c";
}
//edit
a();
console.log(b);
I was wondering if it is possible to set variables in a similar way, but only to the current scope, such as:
function a(){
scope.b="c";
console.log(b);
}
//but "b" is not available here.
Possible use case
var objectOne=new(function(){
this.a="e";
this.b="f";
this.c="g";
})();
(function(){
var importlist=["a","b"];
var scopehere=scope;
for(var importitm of importlist){
scopehere[importitm]=objectOne[importitm];
}
console.log(a,b);
})();
So in this case, it would be possible to "import" many vars serially, have them accessible within the object as vars instead of using "this", (like when you export them to window), but in this case they are private.
In particular, if you are using an html-loaded script and you'd like to make the script properties available in the scope. Examples: paper.js, three.js. Going property by property would be a bit cumbersome, but it could be done automatically if there was such a "scope" object.
I know that you can work around in many ways, and perhaps there are not such substantial benefits to doing this. This question is merely out of curiosity.
Changes to this post:
added a call to function ´a()´ as pointed out by kylestephens.
javascript scope
Just out of curiosity:
In the context of a web browser, it is possible to add a property to the window object, consequently making it global, such as:
function a(){
window.b="c";
}
//edit
a();
console.log(b);
I was wondering if it is possible to set variables in a similar way, but only to the current scope, such as:
function a(){
scope.b="c";
console.log(b);
}
//but "b" is not available here.
Possible use case
var objectOne=new(function(){
this.a="e";
this.b="f";
this.c="g";
})();
(function(){
var importlist=["a","b"];
var scopehere=scope;
for(var importitm of importlist){
scopehere[importitm]=objectOne[importitm];
}
console.log(a,b);
})();
So in this case, it would be possible to "import" many vars serially, have them accessible within the object as vars instead of using "this", (like when you export them to window), but in this case they are private.
In particular, if you are using an html-loaded script and you'd like to make the script properties available in the scope. Examples: paper.js, three.js. Going property by property would be a bit cumbersome, but it could be done automatically if there was such a "scope" object.
I know that you can work around in many ways, and perhaps there are not such substantial benefits to doing this. This question is merely out of curiosity.
Changes to this post:
added a call to function ´a()´ as pointed out by kylestephens.
javascript scope
javascript scope
edited Nov 22 '18 at 11:30
Joaquin
asked Nov 13 '18 at 16:03
JoaquinJoaquin
167
167
JavaScript provides no way of explicitly accessing the local scope (other than direct variable references of course).
– Pointy
Nov 13 '18 at 16:07
add a comment |
JavaScript provides no way of explicitly accessing the local scope (other than direct variable references of course).
– Pointy
Nov 13 '18 at 16:07
JavaScript provides no way of explicitly accessing the local scope (other than direct variable references of course).
– Pointy
Nov 13 '18 at 16:07
JavaScript provides no way of explicitly accessing the local scope (other than direct variable references of course).
– Pointy
Nov 13 '18 at 16:07
add a comment |
2 Answers
2
active
oldest
votes
This is just how scope works —— you don't need to do anything special just define a variable. Variables defined with let
are scoped to their enclosing block. Those with var
are scoped to the function:
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
If you have an object with a bunch of data, it's better to leave that data in some sort of structure rather than a bunch of individual variables. So the idea of importing a bunch of names into a scope isn't supported in general cases. In specific cases you can use destructuring:
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
add a comment |
In theory you can. However, in your first example the a() function is never called and thus the property 'b' is never set on the window object.
It would need to look like this:
function a(){
window.b="c";
}
a();
console.log(b);
Whether you should is another question. It is generally considered bad practice to pollute the global scope in this way. I recommend you look into prototypal inheritance and scopes and how you can use this to your advantage.
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
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%2f53284950%2fis-there-a-way-to-add-a-variable-to-the-scope-in-a-similar-way-to-how-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is just how scope works —— you don't need to do anything special just define a variable. Variables defined with let
are scoped to their enclosing block. Those with var
are scoped to the function:
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
If you have an object with a bunch of data, it's better to leave that data in some sort of structure rather than a bunch of individual variables. So the idea of importing a bunch of names into a scope isn't supported in general cases. In specific cases you can use destructuring:
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
add a comment |
This is just how scope works —— you don't need to do anything special just define a variable. Variables defined with let
are scoped to their enclosing block. Those with var
are scoped to the function:
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
If you have an object with a bunch of data, it's better to leave that data in some sort of structure rather than a bunch of individual variables. So the idea of importing a bunch of names into a scope isn't supported in general cases. In specific cases you can use destructuring:
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
add a comment |
This is just how scope works —— you don't need to do anything special just define a variable. Variables defined with let
are scoped to their enclosing block. Those with var
are scoped to the function:
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
If you have an object with a bunch of data, it's better to leave that data in some sort of structure rather than a bunch of individual variables. So the idea of importing a bunch of names into a scope isn't supported in general cases. In specific cases you can use destructuring:
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
This is just how scope works —— you don't need to do anything special just define a variable. Variables defined with let
are scoped to their enclosing block. Those with var
are scoped to the function:
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
If you have an object with a bunch of data, it's better to leave that data in some sort of structure rather than a bunch of individual variables. So the idea of importing a bunch of names into a scope isn't supported in general cases. In specific cases you can use destructuring:
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
function a(){
let b = "c";
console.log(b);
}
a()
// console.log(b) error -- b not in scope
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
// with array
function scope(importedList){
let [a, b, c] = importedList // a, b, & c are local
console.log(a, b, c)
}
scope([1, 2, 3])
// with object
function objscope(importedObject){
let {a, b, c} = importedObject // a, b, & c are local
console.log(a,b,c)
}
const someObj = {
a: "e",
b: "f",
c: "g"
}
objscope(someObj)
edited Nov 13 '18 at 16:14
answered Nov 13 '18 at 16:07
Mark MeyerMark Meyer
37.8k33159
37.8k33159
add a comment |
add a comment |
In theory you can. However, in your first example the a() function is never called and thus the property 'b' is never set on the window object.
It would need to look like this:
function a(){
window.b="c";
}
a();
console.log(b);
Whether you should is another question. It is generally considered bad practice to pollute the global scope in this way. I recommend you look into prototypal inheritance and scopes and how you can use this to your advantage.
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
add a comment |
In theory you can. However, in your first example the a() function is never called and thus the property 'b' is never set on the window object.
It would need to look like this:
function a(){
window.b="c";
}
a();
console.log(b);
Whether you should is another question. It is generally considered bad practice to pollute the global scope in this way. I recommend you look into prototypal inheritance and scopes and how you can use this to your advantage.
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
add a comment |
In theory you can. However, in your first example the a() function is never called and thus the property 'b' is never set on the window object.
It would need to look like this:
function a(){
window.b="c";
}
a();
console.log(b);
Whether you should is another question. It is generally considered bad practice to pollute the global scope in this way. I recommend you look into prototypal inheritance and scopes and how you can use this to your advantage.
In theory you can. However, in your first example the a() function is never called and thus the property 'b' is never set on the window object.
It would need to look like this:
function a(){
window.b="c";
}
a();
console.log(b);
Whether you should is another question. It is generally considered bad practice to pollute the global scope in this way. I recommend you look into prototypal inheritance and scopes and how you can use this to your advantage.
answered Nov 13 '18 at 16:14
kylestephenskylestephens
159212
159212
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
add a comment |
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
Thanks for pointing that out! I added the call as you suggested.
– Joaquin
Nov 22 '18 at 11:31
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%2f53284950%2fis-there-a-way-to-add-a-variable-to-the-scope-in-a-similar-way-to-how-variables%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
JavaScript provides no way of explicitly accessing the local scope (other than direct variable references of course).
– Pointy
Nov 13 '18 at 16:07