function Person() { this.name = "person"; this.arr = [1]; this.sayName = function() { return this.name; }; } Person.prototype.gender = "man"; function Student() { this.age = "12"; } Student.prototype = new Person(); console.log(Student.prototype.constructor); // f Person() Student.prototype.constructor = Student; //改变原型对象的指向后,会破坏原型链的继承规则,必须手动纠正,要把原型对象的构造函数指向自己的构造函数, var student = new Student(); var person = new Student(); console.log(student.sayName()); //person console.log(student.name); //person` student.name = "student"; console.log(student.name); //student console.log(person.name); //person student.arr.push(2); // console.log(student.arr); //[1,2] console.log(person.arr); //[1,2]
Student.prototype = Person.prototype; Student.prototype.constructor = Student; var student = new Student(); var person = new Person(); console.log(student.gender); //man` Student.prototype.gender = "felman"; // 由于这里的Student.prototype和Person.prototype是共同的引用,因此改变其中一个互相都有形象 console.log(student.gender); //felman console.log(person.gender); //felman 解决办法:取用一个空的函数做为构造函数过分 var F = function() {}; F.prototype = Person.prototype; Student.prototype = new F(); Student.prototype.constructor = Student; var student = new Student(); var person = new Person(); console.log(student.gender); //man` Student.prototype.gender = "felman"; console.log(student.gender); //felman console.log(student.name); //undefined console.log(person.gender); //man // 封装一个基于原型的继承函数 function extend(Child, Person) { var F = function() {}; F.prototype = Person.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; // 建立一个继承指针 }
function Person(val) { this.name = val; this.arr = [1]; this.sayName = function() { return this.name; }; } Person.prototype.gender = "man"; function Student(val) { Person.call(this, val); this.age = "12”; } var student = new Student("student"); var student1 = new Student("student"); console.log(student.name); //student console.log(student.age); //12 console.log(student.sayName()); //student console.log(student.gender) //undefined console.log(student.sayName === student1.sayName); //false
function Person(val) { this.name = val; this.arr = [1]; } Person.prototype.gender = "man"; Person.prototype.sayName = function() { return this.name; }; function Student(val) { Person.call(this, val); this.age = "12"; } Student.prototype = new Person(); Student.prototype.constructor = Student; var student = new Student("student"); var student1 = new Student("student"); console.log(student.name); //student console.log(student.age); //12 console.log(student.gender); //man console.log(student.sayName()); //student console.log(student.sayName === student1.sayName); //true
function Person(val) { this.name = val; this.arr = [1]; } Person.prototype.gender = "man"; Person.prototype.sayName = function() { return this.name; }; function Student(val) { Person.call(this, val); // 继承构造函数的属性 this.age = "12"; } var F=function(){} F.prototype=Person.prototype Student.prototype=new F() // 只继承原型上的属性 Student.prototype.constructor = Student; var student = new Student("student"); var student1 = new Student("student"); console.log(student.name); //student console.log(student.age); //12 console.log(student.gender); //man console.log(student.sayName()); //student console.log(student.sayName === student1.sayName); //true
function checkType(target) { return Object.toString(target).slice(8, -1); } function deepClone(target) { let result; let targetType = checkType(target); if (targetType === "array") { result = []; } else if (targetType === "object") { result = {}; } else { return target; } // 遍历 for (let i in target) { let value = target[i]; // 递归的判断条件为引用类型的时候递归 if (checkType(value) === "Object" || checkType(value) === "Array") { result[i] = deepClone(value); } else { result[i] = value; } } return result; } function Person() { this.name = "person"; this.arr = [1]; this.sayName = function() { return this.name; }; } Person.prototype.gender = "man"; var person = new Person(); var student = deepClone(person); console.log(student.gender); //man console.log(student.name); //person