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