1. How to form a prototype chain?
Case 1
// grand.prototype.__proto__ = object.prototype.grand.prototype.lastName = "Yang"; function Grand() { } var grand = new Grand(); Father.prototype = grand; function Father() { this.name = "handsome"; } var father = new Father(); Son.prototype = father; function Son(){ this.hobby = "somking"; } var son = new Son();Copy the code
2. Add, delete, change and check the prototype chain are the same
Case 2
Grand.prototype.lastName = "Yang";
function Grand() {
}
var grand = new Grand();
Father.prototype = grand;
function Father() {
this.name = "handsome";
this.fortune = {
card1:"visa",
}
}
var father = new Father();
Son.prototype = father;
function Son(){
this.hobby = "somking";
}
var son = new Son();
Copy the code
Example 3
The “this” in sayName refers to whoever called the method
Person.prototype = {
name : "a",
sayName : function() {
console.log(this.name);
}
}
function Person() {
this.name = "b";
}
var person = new Person();
Copy the code
Example 4
Person.prototype = {
height:100,
}
function Person() {
this.eat = function () {
this.height ++;
}
}
var person = new Person();
Copy the code
var obj = {}; Var obj = new Object(); / / prototype obj. __proto__ -- - > Object. The prototypeCopy the code
4.Object.creat(prototype)//// Only null and Object values, not original values
Example 1
var obj = {name:"sunny",age:123}; var obj1 = Object.create(obj); //obj1 is an objectCopy the code
Example 2
Person.prototype.name="sunny"; function Person(){ }// var person = Object.creat(Person.prototype); // It is almost the same as aboveCopy the code
Special case
var obj = Object.create(null);
Copy the code
example
var num = 123; // num.toString(); -->new Number(num).toString(); Number. The prototype. ToString = function () {} Number. The prototype. __proto__ = Object. The prototype/rewrite/methods: the same name in different functionsCopy the code
example
Object. The prototype. ToString = function () {return} 'hahaha'/method/rewrite 1 Person. The prototype = {toString: function () {return "daoshu"; Function Person(){} var Person = new Person(); Object.prototype.toString Number.prototype.toString Array.prototype.toString Boolean.prototype.toString String. The prototype. ToString example 1 Number. The prototype. ToString = function () {return 'laoyangchipi'; } var num =123,num='laoyangchipi'Copy the code
Case 2
var obj = {}; var obj = Object.create(null); ToString = function() {// return 'a firework '; Document.write (obj); // write(obj); // document.write(obj.tostring ()); // The result of the call toString cannot be toString without a prototypeCopy the code
Math.floor is rounded down/math.ceil is rounded up/math.random is arbitrary
example
for(var i = 0; i < 10; i ++){ var num = Math.floor(Math.random()*100); console.log(num); }Copy the code
Call /apply: you can change this to refer to any method
Case 1
function Person(name,age) { // this == obj this.name = name; Obj. name=name this. Age = age; } var person = new person ('xiong',100); var obj = {} Person.call(obj,'yang',100); Person.call()-- Person() is the same thingCopy the code
Case 2
function Person(name,age,sex) { this.name=name; this.age=age; this.sex=sex; } function Student(name,age,sex,tel,grade) { // var this={name:""... } Person.call(this,name,age,sex); // this.tel=tel; this.grade=grade; } var student = new student ('sunny',123,'male',139,2017);Copy the code
Example 3
function Wheel(wheelSize,style) { this.style=style; this.wheelSize=wheelSize; } function Sit(c,sitColor) { this.c=c; this.sitColor=sitColor; } function Model(height,width,len) { this.height=height; this.width=width; this.len=len; } function Car(wheelSize,style,c,sitColor,height,width,len) { Wheel.call(this,wheelSize,style); Sit.call(this,c,sitColor); Model.call(this,height,width,len); } var car = new car (100,' yellow', 'yellow', 1900, 1900, 1900);Copy the code
Apply (this,[wheelSize,style]); arguments wheel.apply (this,[wheelSize,style]); / / case