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

How JScript creates its own objects

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how JScript creates its own objects. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Create your own object

To create your own object instance, you must first define a constructor for it. The constructor creates a new object, assigns properties to the object, and assigns methods when appropriate. For example, the following example defines a constructor for a pasta object. Note the use of the this keyword, which points to the current object.

/ / pasta is a constructor with four parameters.

Function pasta (grain, width, shape, hasEgg)

{

/ / what kind of grain is it made of?

This.grain = grain

/ / how wide is it? (numerical value)

This.width = width

/ / Cross-sectional shape? (string)

This.shape = shape

/ / do you want to add egg yolk? (boolean)

This.hasEgg = hasEgg

}

After defining the object constructor, create an object instance using the new operator.

Var spaghetti = new pasta ("wheat", 0.2, "circle", true)

Var linguine = new pasta ("wheat", 0.3, "oval", true)

You can add properties to an object instance to change the instance, but these attributes are not included in other object definitions generated with the same constructor, and they are not displayed in other instances unless you specifically add them. If you want to display additional properties for all instances of the object, you must add them to the constructor or constructor prototype object (the prototype is discussed in the advanced documentation).

Additional properties of / / spaghetti.

Spaghetti.color = "pale straw"

Spaghetti.drycook = 7

Spaghetti.freshcook = 0.5

Var chowFun = new pasta ("rice", 3, "flat", false)

/ / chowFun object or other existing pasta object

/ / none of them were added to the spaghetti object

Three new properties of / /.

/ / add the attribute 'foodgroup' to the pasta prototype object

/, so that all instances of the pasta object can have this property

/ / include those instances that have been generated.

Pasta.prototype.foodgroup = "carbohydrates"

/ / now spaghetti.foodgroup, chowFun.foodgroup, etc.

/ / all contain the value "carbohydrates".

Include methods in the definition

You can include methods (functions) in the definition of an object. One way is to add an attribute to a constructor that references a function defined elsewhere. For example, the following example extends the pasta constructor defined above to include the toString method, which will be called when the value of the object is displayed.

/ / pasta is a constructor with four parameters.

/ / the first part is the same as above.

Function pasta (grain, width, shape, hasEgg)

{

/ / what kind of grain is it made of?

This.grain = grain

/ / how wide is it? (numerical value)

This.width = width

/ / Cross-sectional shape? (string)

This.shape = shape

/ / do you want to add egg yolk? (boolean)

This.hasEgg = hasEgg

/ / add the toString method here (defined below).

/ / notice that there are no parentheses after the name of the function.

/ / this is not a function call, but

/ / A reference to the function itself.

This.toString = pastaToString

}

/ / the actual function used to display the contents of the past object.

Function pastaToString ()

{

/ / returns the properties of the object.

Return "Grain:" + this.grain + "" +

Width: "+ this.width +"+

Shape: "+ this.shape +"+

"Egg?:" + Boolean (this.hasEgg)

}

Var spaghetti = new pasta ("wheat", 0.2, "circle", true)

/ / will call toString () and display the spaghetti object

Property of / / (requires an Internet browser).

Window.alert (spaghetti)

This is the end of the article on "how JScript creates its own objects". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report