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
function classType(name){ | |
this.name = name; | |
} | |
classType.prototype = { | |
//methods here | |
getName: function(){ | |
return this.name; | |
} | |
}; | |
//or if you want inheritence... | |
function classType(name){ | |
otherClass.call(this, name); | |
this.name = name; | |
} | |
classType.prototype = Object.create(otherClass.prototype); | |
//underscore.js | |
_.mixIn(classType.prototype, { | |
//methods here... | |
getName: function(){ | |
return this.name; | |
} | |
}); | |
//then you have to instantiate it | |
var myobj = new classType("John"); | |
myobj.getName(); | |
//"John" | |
Object.prototype.toString.call(myobj); | |
//[object classType] | |
classType.prototype.isPrototypeOf(myobj); | |
//true |
Instead you probably just wanted this.
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 myobj = { | |
name: "John", | |
getName: function getName(){ | |
return this.name; | |
} | |
} |
Look at how simple that is! It's so much faster. Even to a beginner programmer, the hint that this.name may actually be the name property of the object literal.
Need to do instanceOf? Why not do something like this instead?
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 x = { | |
getName: function(){ | |
return this.name | |
} | |
}; | |
var y = Object.create(x); | |
_.mixIn(y, { | |
name: "John" | |
}); | |
x.isPrototypeOf(y); | |
//true | |
y.getName(); | |
//"John" |
Seriously! There's so much less code. In fact, why not just forget the whole "new" paradigm? Well, maybe not forget, but use it wisely. It also begs the question, how do you make objects repeatedly? Don't you need some kind of overhead? What about applying prototypes to already created objects? Or worse. What about multiple linear inheritance?
Your goal when making applications is get the banana. Not the gorilla, not the jungle, and not everything else. See the following video.
Perhaps it might be overstated that using stampit is quite useful. It is also very close at getting the banana. At 10kb I found that stampit was the correct solution on the back end where we control the modules. Size doesn't matter on the back end. On the front end, it might be more like a fruit salad to a mobile phone. I also have nothing but respect for Eric's library, because it really showcases the fundamental units of object creation.
Could you perhaps have wanted something smaller? (Yeah it's a pretty shameless plug.)
After all, passing callbacks should be really terse.
We should always remember the few components that comprise our objects and functions: Properties, values, closures, functions, prototypes, and contexts.
Edit:
Don't forget to check out the npm registry for my library composer.js.
No comments:
Post a Comment