Home >Web Front-end >JS Tutorial >Chained calls in javascript_javascript skills

Chained calls in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:34:311178browse

The way in jQuery looks like $("#txtName").addClass("err").css("font-size","12px").select().focus(); makes people fascinated by it. The implementation mechanism is chain call. Chain calling is to call the method of an object and then return to the object. Strictly speaking, it does not belong to grammar, but is just a grammar technique. This is the fascinating point of js.
Methods without return values ​​are assignor methods. Obviously, it is easy to implement chain calls, provided that the usage of this pointer is correctly understood.

Copy code The code is as follows:

function W(){
this.name= "Wang Hongjian";
this.gender="male";
}
W.prototype.sayHi=function(){
alert("Hello,everybodynMy name is " this.name);
return this;
};
W.prototype.doSomething=function(){
alert("I'm working");
return this;
}
W.prototype.sayGoodbye=function(){
alert("Goodbye,everybody");
return this;
};
var w=new W();
w.sayHi ().doSomething().sayGoodbye();


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn