Object-oriented writing method in JS

xiaoxiao2021-03-18  230

/ / Define the Circle class, the member function of member variable R, constant PI, and calculation area Area () / 1 // 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 ()); // 2 / 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)); 3 /// var circle = new object (); circle.pi = 3.14159; circle.area = function (r) {return this.pi * R * r;}

Alert (Circle.area (1.0)); / 4 / var circle = {"PI": 3.14159, "Area": ​​Function (R) {Return this.pi * r * r;}}; Alert (Circle.area 1.0)); 5 // var circle = new function ("this.pi = 3.14159; this.area = function (r) {return r * r * this.pi;}"); Alert ((new circle ()) .area (1.0));

转载请注明原文地址:https://www.9cbs.com/read-129959.html

New Post(0)