Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript creates objects and adds methods and properties to them

Shulou Source: shulou.com Published: 2022-06-03 15:01:53 11月14日 Update

This article mainly introduces "how JavaScript creates objects and adds methods and properties for objects". In daily operations, I believe that many people have doubts about how JavaScript creates objects and adds methods and properties for objects. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "how JavaScript creates objects and adds methods and properties for objects". Next, please follow the editor to study!

Create an object, add methods, properties, and dynamically add and delete objects. The sample code is as follows:

Use objects to create objects directly

{

Var circle = {XRL 0THI YRU 0MIT RISUSIRUR 2}

[xss_clean] ("Center at:", circle.x, ", circle.y," Radius: ", circle.radius)

[xss_clean] ("function creates an object")

Function student (name,chinese,math,english)

{

This.name=name

This.chinese = chinese

This.math = math

This.english = english

This.avg2=avg2

}

/ /

Function avg ()

{

Return ((this.chinese + this.math + this.english) / 3)

}

Function avg2 ()

{

Return ((this.chinese + this.math + this.english) / 3) + 5)

}

}

{

Var st1 = new student ("zzc", 88pm 95jue 20)

[xss_clean] ("name" + st1.name + "")

[xss_clean] (Chinese + st1.chinese + "")

[xss_clean] ("Mathematics" + st1.math + "")

[xss_clean] ("English" + st1.english + "")

St1.avg = avg

Var avgscore = st1.avg ()

Var avgscores = st1.avg2 ()

[xss_clean] ("average score" + avgscore + "")

[xss_clean] ("average score + 5 =" + avgscores + "")

[xss_clean] ("Delete object")

[xss_clean] ("st1 object properties and methods:")

For (var i in st1)

[xss_clean] (ST1 [I] + "")

[xss_clean] ("Delete object's property (english) method (avg2)")

Delete st1.avg2

Delete st1.english

For (var i in st1)

[xss_clean] (ST1 [I] + "")

}

At this point, the study on "how JavaScript creates objects and adds methods and properties to objects" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Objects methods properties learning average more help practical next code function dynamic radius center name mathematics articles theory knowledge examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia vpn Xiaomi Microsoft