Reputation: 61
Can anyone please explain to me what is happening in the following code? I understood the meaning of include() and exclude() but when and why do we use the specific function?
var Class = function () {
var klass = function () {
this.init.apply(this, arguments);
};
klass.prototype.init = function () {};
klass.fn = klass.prototype;
//shortcut to access class
klass.fn.parent = klass; //where do we use it?
//adding class properties
klass.extend = function (obj) {
var extended = obj.extended; //what is happening here?
for (var i in obj) {
klass[i] = obj[i];
}
if (extended) extended(klass) //didn't understand this part
};
//adding instance properties
klass.include = function (obj) {
var included = obj.included; //??
for (var i in obj) {
klass.fn[i] = obj[i]; //??
}
if (included) included(klass) //??
};
return klass;
};
var Person = new Class; // is this same as: var Person = new Class();
Upvotes: 4
Views: 1397
Reputation: 451
Would like to offer how I understood it. I would break this into two parts: definition, and implementation (or usage). Following the definition that you have quoted above, I would add usage as this code:
var Person = new Class;
Person.extend({
find: function(id) { /* definition */
},
exists: function(id) { /* definition */
}
});
Person.include({
save: function(id) { /* definition */
},
destroy: function(id) { /* definition */
}
});
var person_e = Person.find(1);
var person_e = Person.exists(2);
var person = new Person;
person.save();
person.destroy(2);
See this code in continuity with the previous code. I'd try to enumerate what happens:
We call new Person() - in effect, we are executing klass, since Person holds klass at this point in time, and since new is attached, 'this' is passed. Now, inside 'klass' initialization happens on init property of 'this' (by apply form) -> And this 'init' property we have just overwritten.
klass.fn.parent = klass; //where do we use it? //no where.
Upvotes: 0
Reputation: 246
After your question-block author give us this part:
Person.extend({
extended: function(klass) {
console.log(klass, " was extended");
}
});
So if we have an 'extended' property in an argument-object - we call it.
Upvotes: 2
Reputation: 889
He got it from Javascript Web Applications by Alex MacCaw. The purpose is to demonstrate implementing Javascript classes, and using proper modularity and inheritance. The author is very abstract in his explanations, and does not specify whether any libraries such as Prototype.js are being used.
I think a good addition to this question would be:
Upvotes: 2