Apple JavaScript Coding Guidelines
developer.apple.com
developer.apple.com
1. It's pretty well accepted that constructors should be capitalized. This is a minor style problem compared to everything else.
2. The entire section on "freeing" objects is just wrong. Not going to bother explaining why.
3. This is wrong:
MyChildObject.prototype = MyParentObject.prototype;
Your entire inheritance tree will share a prototype, and adding a property to one constructor's prototype will add it to all of them, even the parents. You need to do something like "MyChildObject.prototype = new MyParentObject;" or "MyChildObject.prototype = Object.create(MyParentObject.prototype);"4. The copying section contains a for-in loop without a obj.hasOwnProperty(name) guard.
I prefer not to use anonymous function. I will use named function so that my stack trace is comprehensible when I am debugging.
myObject.prototype.memberMethod = function()
{
// code to execute within this method
}
becomes myObject.prototype.memberMethod = function memberMethod()
{
// code to execute within this method
}
Also, what's up with naming the constructor function as 'myObject'. JS prototypical nature is confusing as is and using an example like this can only make it harder to understand.The other option, if you are on Webkit, is to assign something to the `displayName` property of a function or method. This is explained really well here: http://www.alertdebugging.com/2009/04/29/building-a-better-j...
"Assigning a member function to a prototype should not be confused with assigning an anonymous function to a member variable of a class, which only adds the function to a particular instance of the class"
I thought that by setting this.somefn = function() { blah; }; in the constructor, you are adding the function to the prototype of the object, with the name "somefn". At least, I have implemented a fair amount of code this way and used classical JS inheritance without any apparent issues, I would like to know what the difference is both practically and theoretically.
function A () { this.helloWorld = function () {return 'hello world'}; }; a = new A();
vs.
function B () {}; B.prototype.helloWorld = function () {return 'hello world'}; b = new B();
Both constructors achieve the same result - their instances a and b both have the methods helloWorld.
Some of the differences are:
1. With constructor A, whenever you create a new instance, you create a new function, hence you have both performance and memory inefficiencies. With Constructor B, the helloWorld function is created just once, and every instance of B automatically inherits this single instance of the function helloWorld, hence we have performance and memory efficiency.
2. Using inheritance through the prototype of the constructor functions also gives more flexibility and simplicity in sharing common functions and modules.
To demonstrate the concept, I created this code example:
http://jsbin.com/utuso3/edit [Click on 'Preview' button to run the example code]
I hope that made sense.
This somewhat hints to me that Apple will come out with a framework that allows more modular includes or compiles similar to closures system. This is somewhat a hit to jQuery's "all-in-one" setup.
What libraries like Backbone all have in common at the core is that they use setter methods to trigger custom bound listeners/events . When these custom events are fired, they update the multiple variables/objects that are bound directly to them. For someone who is learning JavaScript OOP for the first time on my current project, this makes a lot of sense and seems pretty exciting.