Home >Web Front-end >JS Tutorial >JavaScript object-oriented inheritance method
JavaScript has been around for nearly 20 years, but there are still different opinions on this prophecy. Many people say that JavaScript cannot be considered an object-oriented programming language. But JavaScript is very loosely typed and has no compiler. This gives programmers a lot of freedom, but also brings some flaws.
Although JavaScript is not an object-oriented language. But we can implement JavaScript's object-oriented programming by imitating the way other languages implement object-oriented programming.
The following is a very classic inheritance method in JavaScript tutorials.
//定义一个Pet对象。通过这一个名称和数量的腿。 var Pet = function (name,legs) { this.name = name; //Save ths name and legs values. this.legs = legs; }; //创建一个方法,显示了Pet的名字和数量的腿。 Pet.prototype.getDetails = function () { return this.name + " has " + this.legs + " legs "; } //定义一个Cat对象,继承从Pet。 var Cat = function (name) { Pet.call(this,name,4); //调用这个父对象的构造函数 }; //这条线执行继承从Pet。 Cat.prototype = new Pet(); //增加一个动作方法的猫 Cat.prototype.action = function () { return "Catch a bird"; }; //创建一个实例petCat的猫。 var petCat = new Cat("felix"); var details = petCat.getDetails(); console.log(details) //"felix has 4 legs". var action = petCat.action(); console.log(action) //"Catch a bird". petCat.name = "sylvester"; //改变petCat的名字 petCat.legs = 7; //改变petCat腿的数量 details = petCat.getDetails(); console.log(details) //"sylvester has 7 legs". //定义一个Pet对象。通过这一个名称和数量的腿。 var Pet = function (name,legs) { this.name = name; //Save ths name and legs values. this.legs = legs; }; //创建一个方法,显示了Pet的名字和数量的腿。 Pet.prototype.getDetails = function () { return this.name + " has " + this.legs + " legs "; } //定义一个Cat对象,继承从Pet。 var Cat = function (name) { Pet.call(this,name,4); //调用这个父对象的构造函数 }; //这条线执行继承从Pet。 Cat.prototype = new Pet(); //增加一个动作方法的猫 Cat.prototype.action = function () { return "Catch a bird"; }; //创建一个实例petCat的猫。 var petCat = new Cat("felix"); var details = petCat.getDetails(); console.log(details) //"felix has 4 legs". var action = petCat.action(); console.log(action) //"Catch a bird". petCat.name = "sylvester"; //改变petCat的名字 petCat.legs = 7; //改变petCat腿的数量 details = petCat.getDetails(); console.log(details) //"sylvester has 7 legs".
Although there is no big problem in executing the above method, the overall style of the code is slightly bloated and not very elegant. Properties can still be modified outside. This approach does not protect inherited properties. The following method omits new and prototype and uses the feature of "function inheritance" to implement it.
//定义一个pet对象。通过这一个名称和数量的腿。 var pet = function (name,legs) { //创建一个对象that,其中名字是可以改的,但是腿数不可以改,实现了变量私有化。 var that = { name : name, getDetails : function () { return that.name + " has " + legs + " legs "; } }; return that; } //定义一个cat对象,继承从pet。 var cat = function (name) { var that = pet(name,4); //从pet中继承属性 //cat中增加一个action的方法。 that.action = function () { return "Catch a bird"; } return that; } //创建一个petCat2; var petCat2 = cat("Felix"); var details = petCat2.getDetails(); console.log(details) //"felix has 4 legs". var action = petCat2.action(); console.log(action) //"Catch a bird". petCat2.name = "sylvester"; //我们可以改变名字。 petCat2.legs = 7; //但是不可以改变腿的数量 details = petCat2.getDetails(); console.log(details) //"sylvester has 4 legs". //定义一个pet对象。通过这一个名称和数量的腿。 var pet = function (name,legs) { //创建一个对象that,其中名字是可以改的,但是腿数不可以改,实现了变量私有化。 var that = { name : name, getDetails : function () { return that.name + " has " + legs + " legs "; } }; return that; } //定义一个cat对象,继承从pet。 var cat = function (name) { var that = pet(name,4); //从pet中继承属性 //cat中增加一个action的方法。 that.action = function () { return "Catch a bird"; } return that; } //创建一个petCat2; var petCat2 = cat("Felix"); var details = petCat2.getDetails(); console.log(details) //"felix has 4 legs". var action = petCat2.action(); console.log(action) //"Catch a bird". petCat2.name = "sylvester"; //我们可以改变名字。 petCat2.legs = 7; //但是不可以改变腿的数量 details = petCat2.getDetails(); console.log(details) //"sylvester has 4 legs".
Warm reminder: The advantage of using prototypal inheritance is that it is memory efficient. No matter how many times it is inherited, the prototype properties and methods of the object are only saved once. When functions are inherited, duplicate properties and methods are created for each new instance. If you create many large objects, memory consumption will be very large. The solution is to save the larger properties or methods in an object and pass it as a parameter to the constructor. This way all instances will use one object resource instead of creating their own versions.
The above two methods can easily implement JavaScript object-oriented inheritance. No method is absolutely good, and no method is absolutely bad. Depends on personal preference.