Created
August 18, 2010 13:08
-
-
Save torgeir/534661 to your computer and use it in GitHub Desktop.
js functional+prototypal inheritance template
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
var sys = require('sys'); | |
var createObject = function (obj) { | |
function F () {}; | |
F.prototype = obj; | |
return new F; | |
}; | |
var button = (function () { | |
var defaultName = ''; | |
var create = function (name) { | |
var button = createObject(buttonPrototype); | |
button.name = name || defaultName; | |
return button; | |
}; | |
var buttonPrototype = { | |
toString: function () { | |
return 'Us buttons share this anonymous method!! ' + this.name; | |
} | |
}; | |
return { | |
create: create | |
}; | |
})(); | |
var a = button.create('a'); | |
var b = button.create('b'); | |
var c = { name: "I don't, I am [object Object]" }; | |
sys.puts(a); | |
sys.puts(b); | |
sys.puts(c); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment