Prototype object in JS

Understanding prototype objects

Each function has a prototype property that points to the prototype object of the function.By default, all prototype objects automatically get a constructor property, which is a pointer to the function in which the prototype property resides.

for example

function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayname = sayName;
    }
function sayName(){
    alert(this.name);
    }
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", "27", "Doctor");

Person.prototypr.constructor points to Person.With this constructor, we can also add additional properties and methods to the prototype object.

function Person( ){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function ( ) {
    alert(this.name);
}
var person1=new Person( );
person1.sayName( ); //"Nicholas"

var person2 = new Person( );
person2.sayName( ); //"Nicholas"

Although a Prototype cannot be accessed directly, the isPrototypeOf() method can be used to determine whether this relationship exists between objects.

alert(Person.prototype.isPrototypeOf(person1)); //true
alert(Person.prototype.isPrototypeOf(person2)); //true

A new method, Object.getPrototypeOf, has been added to ECMAScript 5 to return the value of Prototype.

alert(Object.getPrototypeOf(person1)==Person.prototype); //true
alert(Object.getPrototypeOf(person1).name); //"Nocholas"

Although values in the prototype can be accessed through object instances, values in the prototype cannot be overridden through object instances.Attributes with the same name as the prototype added in the instance will override that attribute in the prototype, and the delete operator will completely remove the instance attribute, allowing us to access the prototype attribute.

function Person( ){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function ( ) {
    alert(this.name);
}

var person1 = new Person( );
var person2 = new Person( );

person1.name = "Greg";
alert(person1.name);                  //"Greg"
alert(person2.name);                 //"Nicholas"

delete.person1.name;
alert(person1.name);                  //"Nicholas"

Using the hasOwnProperty() method (inherited from Object) you can detect whether an attribute exists in an instance.

function Person( ){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function ( ) {
    alert(this.name);
}

var person1 = new Person( );
alert(person1.hasOwnProperty("name"));     //false

person1.name = "Greg";
alert(person1.name);      //"Greg"
alert(person1.hasOwnProperty("name"));     //true

delete person1.name;
alert(person1.hasOwnProperty("name"));     //false

Prototype and in operator

Not Ended to Continue

Tags: Attribute ECMAScript

Posted on Wed, 13 May 2020 09:30:32 -0700 by rob323