Created
May 25, 2013 20:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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