English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
//Define the Circle class with member variable r, constant PI, and member function area() to calculate the area
1.Factory method
var Circle = function() { var obj = new Object(); obj.PI = 3.14159; obj.area = function( r ) { return this.PI * r * r; } return obj; } var c = new Circle(); alert( c.area( 1.0 ) );
2.Regular syntax
function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { return Circle.PI; * this.r * this.r; } var c = new Circle();1.0); alert(c.area());
3.JSON syntax
var Circle={}} "PI":3.14159, "area":function(r){ return this.PI * r * r; } }); alert( Circle.area(1.0) );
4. A little change, but the essence is the same as the first
var Circle=function(r){ this.r=r; } Circle.PI = 3.14159; Circle.prototype={ area:function(){ return this.r*this.r*Circle.PI; } } var obj=new Circle(1.0); alert(obj.area());
Circle.PI = 3.14159; Can be written into the attribute as this.PI=3.14159;
Commonly used are the first and third
An extended small example of the third writing method
var show={ btn:$('.div1'), init:function(){ var that=this; alert(this); this.btn.click(function(){ that.change(); alert(this); }); , change:function(){ this.btn.css({'background':'green'}); } } show.init();
That's all for this article. I hope the content of this article can bring a certain amount of help to everyone's learning or work, and I also hope to support the呐喊 tutorial more!
Declaration: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any suspected copyright content, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)