Changing a global JavaScript object?

Is there a way to change the root object in JavaScript?

For example, in browsers, the root object is a "window". So,

X = 5; console.log(Y); 

matches with:

 window.X = 5; console.log(window.Y); 

What I want to do now is change this root object, so when I do the following:

 X = 6; 

The reason I need this:

In a Node.js application, each part of the program can access a global object. This is a big problem because every script that is executed by the Node.js web server can add new variables to it. They will be there until the web server restarts. I want to avoid this by changing the global object.

Update

I checked the following code and got a really interesting result. What did you expect from the following code?

 var X = {A:"a",B:"b"}; with(X){ A = 5; C = 7; } for(a in X){ console.log(a+" is "+X[a]); } /* Expected Console Output: A is 5 B is b C is 7 Real Console Output: A is 5; B is b; */ 

Is there any way to get the output as I expected?

Update

Now I have tested a modular system with the following code.

 //program.js var t = require("./module.js"); t.Test(); console.log(A); //module.js A = 5; exports.Test = function(){ console.log("hello world!"); } 

The output was:

 hello world! 5 

This tells me that the variable "A" defined in module.js been added to the global program.js object. The module also does not solve my problem.

+6
javascript
source share
3 answers

There is with , but it is not recommended and is prohibited in strict mode.

It is better to refer to a variable containing the object explicitly.

In response to an updated question:

with will search for a chain of scopes until it finds an object with the corresponding property or reaches window . This is not useful for defining new object properties.

 var X = { A: 5, B: 8, C: 7}; with(X){ console.log(A, B, C); } 
+6
source share

If you are talking about variables, JavasScript has a scope.

 X = 5; // global variable console.log( window.X ); // 5 (function() { var X = 6; // declare a local variable by using the "var" keyword console.log( X ); // 6 })(); console.log( window.X ); // 5 

Otherwise, you can create an object and add properties to it.

 X = 5; console.log( window.X ); // 5 var obj = {}; obj.X = 6; console.log( obj.X ); // 6 console.log( window.X ); // 5 

EDIT: Adding another possible solution that can be used.

You can call an anonymous function, but set the function context to your X object. Then this in function will refer to X

 var X = {}; (function(){ this.A = 5; this.B = 8; this.C = 7; }).call(X); for(a in X){ console.log(a+" is "+X[a]); } 

The .call() method (as well as the .apply() method) allows you to explicitly indicate that thisArg of a calling context. The first argument you pass will be how of a calling context. The first argument you pass will be how this` is defined in the context of the call.

Or just pass X as an argument.

 var X = {}; (function(X){ XA = 5; XB = 8; XC = 7; })(X); for(a in X){ console.log(a+" is "+X[a]); } 

Although it’s easiest to just refer to it (as I already noted in my answer above).

 var X = {}; XA = 5; XB = 8; XC = 7; for(a in X){ console.log(a+" is "+X[a]); } 

or use the module template:

  /****** I'm guessing at the use of "global" here ********/ global.myNamespace = (function(global,undefined) { // define the object to be returned var X = {}; // define private local variables var a_local = 'some value'; var another_local = 'some other value'; // define private functions function myFunc() { // do something with local variables } // give the return object public members X.someProperty = 'some value'; X.anotherProperty = 'another value'; X.publicFunc = function() { //do something with the local variables // or public properties }; X.anotherFunc = function() { //do something with the local variables // or public properties }; // return the object return X; })(global); console.log(myNamespace); 
+5
source share

Sorry, but at least for HTML pages you cannot! You should probably use closures or namespaces !

0
source share

All Articles