var Vehicle = new Class({
drive : function() {
console.log("I'm driving a " + this + " with a top speed of " + this.topSpeed);
},
});
var Car = new Class({
Extends : Vehicle,
topSpeed : 250,
initialize : function(make, model) {
this.make = make;
this.model = model;
},
toString : function() {
return this.make + " " + this.model;
},
});
new Car("Fjord", "Taunus").drive();
Then I went on to server-side JavaScript, namely on node.js. There I immediately felt a bit weak without the Class() constructor. I did some googling to see if I could have Mootools on server-side too, but got under the impression that it's not that easy, requiring some customization of Mootools. Hence, I decided to roll my own. And here it is:
function Class(props) {
function copyProperties(from, to) {
for (var key in from) {
to[key] = from[key];
}
}
var constructor = function() {
props.initialize.apply(this, arguments);
};
if (props.Extends) {
copyProperties(props.Extends.prototype, constructor.prototype);
}
copyProperties(props, constructor.prototype);
return constructor;
}
That's not so much code after all, is it? The code in the first snippet actually works with my Class constructor. As you can see, it supports subclassing using 'Extends', a constructor named 'initialize' and any number of methods and properties.
It took me a couple of hours to get these roughly 10 lines of code together though. I had to realise that
- The "class" is actually a constructor function. Hence, the Class() function has to return a constructor
- In any Javascript function, you have an "arguments" array (well not stricly an array but..) that you can use to access all arguments
- You can call any Function using its apply function with the "this" object and a list of arguments
- You can easily copy properties of an object by looping them using "for key in object"
I have to admit that I'm still a bit confused how this all works though :)
Feel free to prove my über-simple class system wrong and point me back to the correct path.
I enjoyed your blog Thanks for sharing such an informative post. We are also providing the best services click on below links to visit our website.
ReplyDeletedigital marketing company in nagercoil
digital marketing services in nagercoil
digital marketing agency in nagercoil
best marketing services in nagercoil
SEO company in nagercoil
SEO services in nagercoil
social media marketing in nagercoil
social media company in nagercoil
PPC services in nagercoil
digital marketing company in velachery
digital marketing company in velachery
digital marketing services in velachery
digital marketing agency in velachery
SEO company in velachery
SEO services in velachery
social media marketing in velachery
social media company in velachery
PPC services in velachery
online advertisement services in velachery
online advertisement services in nagercoil
web design company in nagercoil
web development company in nagercoil
website design company in nagercoil
website development company in nagercoil
web designing company in nagercoil
website designing company in nagercoil
best web design company in nagercoil
web design company in velachery
web development company in velachery
website design company in velachery
website development company in velachery
web designing company in velachery
website designing company in velachery
best web design company in velachery
Thanks for Sharing - ( Groarz branding solutions )