Home > Web Front-end > JS Tutorial > body text

Chained calls in javascript_javascript skills

WBOY
Release: 2016-05-16 18:34:31
Original
1150 people have browsed it

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();


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template