Possible Duplicate:
Javascript syntax I haven't seen till now, what does it do really?
I was checking out a library called def.js which makes JavaScript objects inherit in a similar fashion as Ruby. But the thing that I couldn’t really get was the way JavaScript was used in the example provided:
def ("Person") ({
init: function(name){
this.name = name;
},
speak: function(text){
alert(text || "Hi, my name is " + this.name);
}
});
def ("Ninja") << Person ({
init: function(name){
this._super();
},
kick: function(){
this.speak("I kick u!");
}
});
var ninjy = new Ninja("JDD");
ninjy.speak();
ninjy.kick();
in short, the two points are:
- def (“Person”)({}); // parentheses after function call
- def (“Ninja”) << Person ({}); // two function calls seperated by the operator <<
Is this a correct/legal use of JavaScript, and if it is, what’s it’s meaning i.e. how is interpreted by the browser.
is basically chained function calls. It means:
"Person"to the function calldef().def()returns a function which can then be called.{...}as an argument to the function that is returned bydef().Not sure about the
<<operator as I’ve not encountered it in JS before. I would think it’s the left bit-shift operator, but I don’t know how it applies to functions.