javascript - Encapsulating functions and variables in an object with object-wide variables -


So, I want to create an object that has functions and variables and objects that contain functions and variables, but I think that if there are "top-level" variables, due to the scoping rules, they can not be reached with deeper work. For example:

  var myobj = {foo: "test", bar: function () {return.foo; }, Falcon: {pizz: "test 2", pause: function () {this.piz return this; Foo; }}} Console.log (myobj.bar ()); Console.log (myobj.baz.poz ());  

The second call returns for obvious reasons "test2undefined" - this.foo refers to baz.foo (which is undefined) and not myobj I have variable fu available everywhere in my object How can I reference myobj to allow?

You can modify your by pause () to:

  pause: function () {this.piz return + myobj.foo; }  

This can not reference two objects, so that's why I'm just using myobj.foo .

If you wanted to refer to myobj in this context, then I suppose, why not use the property of the object?

Note that you can always refer to an object by referencing the original object name in anywhere code.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -