Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Repeat or replace the type data of creating car with different functions of the object function itself (development)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Create the data type program of Car / / function car () {} / / create the shared method car. Prototype. Shift = function () {alert (this. Color + "car");} selection format is more common in C++ code, the format can be similar to updates such as the following: / / create car type function car () {if (car. Prototype shift) {/ / car can only be executed once within an if statement. Prototype.shift = function () {alert (this. Color + "car") The corresponding replacement objects that are relatively different are as follows: the resulting function dyadic Using the object function to form the function of function itself with the type of function car And the prototype attribute can be extended and replaced with each other. / / create the car type function Car () {this. Color = null; / / this can be omitted. Shift = function () {alert (this. Color + "car");}} var redCar = new Car (); redCar. Color = "red"; var blueCar = new Car (); blueCar. Color = "blue"; redCar. Shift (); blueCar. Shift (); / / create object functional function plane () {this of type Plane. Shift = function () {alert (this. Color + "aircraft");}} var redplane = new plane (); redplane. Color = "red"; var blueplane = new plane (); blueplane. Color = "blue"; the construction object generates the value redplane in the function. Shift (); blueplane. Shift (); call the execution procedure of the constructor / / function value 3 function constructor () {this. A = 3;} / / Constructor value + a var obj, var obj = new Constructor (); alert ("a:" + obj. A + "\ nb:" + obj. B + "\ nConstructor:" + obj. Constructor); rewrite the entire cardinality code / / O object. Prototype. Shift = funticon () {alert (this. Color);} var redCar = new object (); redCar. Color = "red"; var blueCar = new object (); blueCar. Color = "blue"; redCar shift (); blueCar shift () The above code extends the prototype of the Object constructor, the shift formula can be used repeatedly, if you want to create a shift formula with different behavior to another type of object such as aircraft or car, because the attribute has the same name, only in the built-in constructor Prototype can only be extended, can not be replaced.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report