Get the App
SLTechnology News&Howtos  ›  Development  › 

JavaScript defines instances of prototype objects and inherited objects

Shulou Source: shulou.com Published: 2022-06-03 15:02:23 10月02日 Update

This article mainly explains the "JavaScript definition prototype object and inheritance object example", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "JavaScript definition prototype object and inheritance object instance"!

Define prototype objects and inherit objects. The sample code is as follows:

Prototype _ object _ inheritance

{

Function Circle (xmemy r)

{

This.x=x

This.y=y

This.r=r

}

Circle.prototype.pi = 3.1415926

Function Circle_circumference ()

{

Return 2*this.pi*this.r

}

Circle.prototype.circumference = Circle_circumference

Var c = new Circle (0.0,0.0,5)

Var p = c.circumference ()

[xss_clean] ("the perimeter of circle c is Circle (0.0,0.0,5):", p)

[xss_clean] ("inheritance")

Function Circle_area ()

{

Return this.pi*this.r*this.r

}

Circle.prototype.area = Circle_area

Var mji = c.area ()

[xss_clean] ("the area of circle c is:", mji)

[xss_clean] ("redefinition: do not inherit Circle (0.0,0.0,10);")

Var C3 = new Circle (0.0,0.0,10)

Function area ()

{

Return 3*this.pi*this.r*this.r

}

C3.area = area

P = C3.circumference ()

Mji = C3.area ()

[xss_clean] ("the circumference of the C3 circle is:", p)

[xss_clean] ("the area of C3 circle is:", mji)

}

Thank you for reading, the above is the content of "JavaScript defines instances of prototype objects and inherited objects". After the study of this article, I believe you have a deeper understanding of the problem of defining instances of prototype objects and inherited objects by JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Objects prototypes examples learning content perimeter area code that is ideas situations articles more knowledge knowledge points examples articles follow questions practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology MySQL OPPO Reno NVidia