Skip to content

Instantly share code, notes, and snippets.

@justinwinslow
Created May 25, 2013 20:38
Show Gist options
  • Save justinwinslow/5650704 to your computer and use it in GitHub Desktop.
Save justinwinslow/5650704 to your computer and use it in GitHub Desktop.
Example of public/private properties in constructors and the difference between apply/call
//New constructor
var cons = function(){
var property_private = 'bye';
this.property_public = 'see ya';
//Instead of returning this, return only what needs
//to be accessible
return {
property_public: this.property_public,
method_public: function(){
console.log(property_private);
}
};
}
//Instantiate object
var myCons = new cons();
//Function to apply/call
var func = function(arg1, arg2){
console.log(arg1, arg2, this.property_public, this.property_private);
this.method_public();
}
func.call(myCons, 'call', 'jwin');
//call jwin see ya undefined
//bye
func.apply(myCons, ['apply', 'jwin']);
//apply jwin see ya undefined
//bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment