Javascript- inherited prototype object

I blame zero reading comprehension test, which looked at the book hit the skull.

Prototype object:

function laptop(color, brand, price){

  this.color = color;

  this.brand = brand;

  this.price = price;

}

console.log(laptop.prototype);

The output is a prototype object, the original point inside a constructor function.

console.log(laptop === laptop.prototype.constructor)    //true

console.log(laptop === laptop.prototype);                     // false

 

Cry, as explained in this book with tongue twisters:

  "When we create a function, javascript will automatically create a prototype object, the function will be created by default have a ptototype attribute points to the prototype object, the prototype object also has a constructor property that points to the original function.

Therefore exists for a prototype object constructor prototype object accessible through. "

Could it be said: 1.prototype property, create a function to it from the prototype object. laptop with laptop.prototype, they are not the same thing.

       2.constructor properties from the prototype object points of the original function. laptop and laptop.prototype.constructor the same thing.

       3. After changing the properties of the prototype object, and does not change the original function, but the function with the instantiated object may be changed.

Laptop function (Color, Brand,. price) {
  this.color = Color;
  this.brand = Brand;
  this.price =. price;
  this.program = function () {
    the console.log ( "for playing games");
    }
}
Laptop new new laptop1 = var ( "White", "Apple", "10000");
laptop.prototype.size = 15;   
laptop.prototype.game function = () {
  the console.log ( "to draw");
}        // only add a new property on the prototype object does not change the original function
console.log (laptop.prototype)

console.log(laptop);
console.log(laptop.size)
console.log(laptop1.size);

Wrote the book "After obtaining the prototype object can change all the methods and properties to the prototype object as the prototype of an object by modifying the prototype object's methods and properties."

cry.

Perhaps it might probably mean is: instantiate If you find the original function does not have this property, then to find the prototype object.

Achieved: After changing the properties of the prototype object, and does not change the original function, but the function with the instantiated object may be changed.

Prototype object inheritance:

"Specify a prototype object to the constructor, all the properties and methods of objects created using this constructor will have prototype object, which is inherited."

Specify a prototype object to the constructor? (Specify a constructor for the prototype objects)? ?

 

 Similarly, instantiated not on the original function, go to the prototype object to find.

ul1 = new ul();

console.log(ul1.color);    // white
console.log(ul1.brand);  //acer
console.log(ul1.size);     //15

 

Guess you like

Origin www.cnblogs.com/mingnai/p/11986370.html