javascript_原型繼承
阿新 • • 發佈:2018-05-04
turn primary || ret scrip 方法 ava cal java
//javascript_原型繼承 //--------------------------------------代碼1: ‘use strict‘ function inherits(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; } function Student(props) { this.name = props.name || ‘Unnamed‘; } Student.prototype.hello = function () { alert(‘Hello, ‘ + this.name + ‘!‘); } function PrimaryStudent(props) { Student.call(this, props); this.grade = props.grade || 1; } // 實現原型繼承鏈: inherits(PrimaryStudent, Student); // 綁定其他方法到PrimaryStudent原型: PrimaryStudent.prototype.getGrade = function () { return this.grade; }; //--------------------------------------代碼1解說: //1.inherits()方法復用實現原型繼承
javascript_原型繼承