Because JavaScript is based on prototypes and does not have the concept of classes (ES6 has it, let’s not talk about it for now), all we have access to are objects, and everything is truly an object
So when we talk about objects, it is a bit vague. Many students will confuse the concepts of typed objects and objects themselves. We will not mention objects in the following terminology. We use a method similar to Java to facilitate understanding
Method 1
Class (function simulation)
function Person(name,id){ //实例变量可以被继承 this.name = name; //私有变量无法被继承 var id = id; //私有函数无法被继承 function speak(){ alert("person1"); } } //静态变量,无法被继承 Person.age = 18; //公有函数可以被继承 Person.prototype.say = function(){ alert("person2"); }
Inherit and call the parent class method
function Person(name,id){ //实例变量可以被继承 this.name = name; //私有变量无法被继承 var id = id; //私有函数无法被继承 function speak(){ alert("person1"); } } //静态变量,无法被继承 Person.age = 18; //公有静态成员可被继承 Person.prototype.sex = "男"; //公有静态函数可以被继承 Person.prototype.say = function(){ alert("person2"); } //创建子类 function Student(){ } //继承person Student.prototype = new Person("iwen",1); //修改因继承导致的constructor变化 Student.prototype.constructor = Student; var s = new Student(); alert(s.name);//iwen alert(s instanceof Person);//true s.say();//person2
Inherit, override the parent class method and implement super()
function Person(name,id){ //实例变量可以被继承 this.name = name; //私有变量无法被继承 var id = id; //私有函数无法被继承 function speak(){ alert("person1"); } } //静态变量,无法被继承 Person.age = 18; //公有静态成员可被继承 Person.prototype.sex = "男"; //公有静态函数可以被继承 Person.prototype.say = function(){ alert("person2"); } //创建子类 function Student(){ } //继承person Student.prototype = new Person("iwen",1); //修改因继承导致的constructor变化 Student.prototype.constructor = Student; //保存父类的引用 var superPerson = Student.prototype.say; //复写父类的方法 Student.prototype.say = function(){ //调用父类的方法 superPerson.call(this); alert("Student"); } //创建实例测试 var s = new Student(); alert(s instanceof Person);//true s.say();//person2 student
Inherited wrapper function
function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; }
uber means setting an uber attribute for the child object, which directly points to the prototype attribute of the parent object. (Uber is a German word meaning "up" or "one level up.") This is equivalent to opening a channel on the child object, and you can directly call the parent object's method. This line is placed here just to achieve the completeness of inheritance and is purely for backup purposes.
Method 2
Equivalent to copying, the object you want to inherit is carried through the defined _this object. In this case, inheritance can be achieved by passing _this, which is easier to understand than the above
//创建父类 function Person(name,id){ //创建一个对象来承载父类所有公有东西 //也就是说_this承载的对象才会被传递给子类 var _this = {}; _this.name = name; //这样的是不会传递下去的 this.id = id; //承载方法 _this.say = function(){ alert("Person"); } //返回_this对象 return _this; } //子类 function Student(){ //获取person的_this对象,从而模仿继承 var _this = Person("iwne",1); //保存父类的_this引用 var superPerson = _this.say; //复写父类的方法 _this.say = function(){ //执行父类的say superPerson.call(_this); alert("Student"); } return _this; } var s = new Student(); s.say();
I hope it will be helpful for everyone to learn javascript programming.