Skip to content

Instantly share code, notes, and snippets.

@NoMan2000
Created October 30, 2015 14:50
Show Gist options
  • Save NoMan2000/44d746d3e964832a021e to your computer and use it in GitHub Desktop.
Save NoMan2000/44d746d3e964832a021e to your computer and use it in GitHub Desktop.
JavaScript create a virtual object
var Foo = function(){
this.par = 3;
this.sub = new(function(t){ //using virtual function to create sub object and pass parent object via 't'
this.p = t;
this.subFunction = function(){
alert(this.p.par);
}
})(this);
}
var myObj = new Foo();
myObj.sub.subFunction() // will popup 3;
myObj.par = 5;
myObj.sub.subFunction() // will popup 5;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment