Home > Web Front-end > JS Tutorial > body text

What is the prototype of js

(*-*)浩
Release: 2019-06-15 17:58:34
Original
4826 people have browsed it

Javascript is also an object-oriented language, but it is a prototype-based language rather than a class-based language.

What is the prototype of js

As mentioned in the article Creating Objects in JavaScript: There is a problem with using constructors to create objects, that is, the same methods of different instances of the same constructor are different, so we Use prototypes to extract the public properties and methods in the constructor and encapsulate them so that they can be shared by all instances.

The object defined by function has a prototype attribute, and the prototype attribute points to a prototype object. There is a constructor attribute in the prototype object. This constructor attribute also points to a constructor object, and this constructor object is exactly the function itself.

The pseudo code is expressed as follows:

var function{
    prototype:prototype{
        constructor:constructor == function
    }
}
Copy after login

The relationship between function and prototype

#Creating a function in js will A prototype attribute is automatically created, which points to the prototype object of the function, and the prototype object automatically obtains a constructor attribute pointing to the function.

Example: Take the previous prototype mode to create an object as an example to illustrate

<script type="text/javascript">
function Person(){

}
Person.prototype.name="lxy";
Person.prototype.age=22;
Person.prototype.job="Software Engineer";
Person.prototype.sayName=function(){
    alert(this.name);
}
     
     var lxy=new Person();
     lxy.sayName();
     var personA=new Person();
     personA.sayName();
     alert(lxy.sayName()==personA.sayName());//true
</script>
Copy after login

The relationship between the instance and the prototype

Create an instance through the constructor. The instance will contain a property (pointer) inside that points to the prototype object of the constructor.

Example: The [[Prototype]] attributes of Person constructor instances Person1 and Person2 both point to the prototype of Person.

Note: The [[Prototype]] connection exists between the instance and the prototype of the constructor, not between the instance and the constructor.

Regarding this pointer, it is called [[Prototype]] in ECMA-262. There is no standard way to access [[Prototype]], but Firefox, Safari and Chrome support an attribute __protp__ on each object. In other implementations, this property is not visible to scripts.

The above is the detailed content of What is the prototype of js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template