通过原型扩展JavaScript中的对象功能



我正在自学JavaScript,我正在寻找从对象继承并扩展其功能的方法。。。和哇!事实证明有几种方法可以做到这一点。

因此,有一个流行的方法是通过extend和class关键字来扩展对象(简而言之(,我在下面举了一个我所看到的例子。好吧,由于我来自C++Python语言,这确实是!非常有用。。。但我知道这是糖代码,可以帮助来自面向对象语言的程序员感觉JavaScript更舒适。所以我的目标是了解如何在不使用上述方法的情况下扩展对象功能,因为我渴望了解js是如何在后台工作的(至少对它有很大的了解(并对此感到满意。

备注

我知道这里有关于这个话题的帖子,但我认为这些帖子不符合我的需求,因为那些(非常好的(深入研究的帖子是2012年左右的。

以类和扩展关键字的方式

参考:https://www.geeksforgeeks.org/how-to-extend-an-object-in-javascript/

// Declaring class 
class Profile {
// Constructor of profile class 
constructor(name, age) {
this.name = name;
this.age = age;
}
getName() {
// Method to return name 
return this.name;
}
getAge() {
// Method to return age 
return this.age;
}
getClass() {
return this;
}
}
// Class Student extends class Profile  
class Student extends Profile {
// Each data of class Profile can be 
// accessed from student class. 
constructor(name, age, languages) {
// Acquiring of attributes of parent class 
super(name, age);
this.lang = [...languages];
}
// Method to display all attributes 
getDetails() {
console.log("Name : " + this.name);
console.log("Age : " + this.age);
console.log("Languages : " + this.lang);
}
}
// Creating object of child class with passing of values 
var student1 = new Student("Ankit Dholakia", 24,
['Java', 'Python', 'PHP', 'JavaScript']);
student1.getDetails();

我试过这个。。。但是我不舒服

function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.selfPresentation = function() {
console.log("Hello, my name is " + this.name + "and I'm " + this.age + " years old.");
}
function Worker(name, age, occupation) {
Person.call(this, name, age);
this.occupation = occupation;
}
Worker.prototype = new Person;
Worker.prototype.selfPresentation = function() {
// I think there undoubtedly is a best approach for this...
this.__proto__.__proto__.selfPresentation.call(this); 
console.log("I don't breath only, I'm a " + this.occupation);
}
let variable = new Worker("Arturo", 22, "Student");
variable.selfPresentation();

(这有点偏离了我的目标,但..(对于对象之间的继承,我试图模仿Object.create方法

a = {
field: "field",
method: function() {
console.log("SuperMethod");
}
}
function inheritHimAll(object) {
function helperFunction() {}
helperFunction.prototype = object;
return new helperFunction;
}
b = inheritHimAll(a);
console.log(b.__proto__)

在javascript中扩展对象的最佳方法是什么?

在javascript中扩展对象的最佳方法是什么?

这是一个非常主观的问题,但我尽可能使用class语法。它们更直观,并且需要更少的样板代码。

但是,我认为您更感兴趣的是了解如何在不使用class语法的情况下执行继承。以下是我将从您的示例中更改的内容:

  1. 不要为prototype创建类的新实例。属性都存在于对象中,而不是原型中。相反,使用Object.create()创建一个具有类原型的新对象。有关此问题的详细信息,请参阅此答案
  2. 切勿使用__proto__。相反,从类的原型手动调用函数或使用Object.getPrototypeOf()
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.selfPresentation = function() {
console.log("Hello, my name is " + this.name + "and I'm " + this.age + " years old.");
}
function Worker(name, age, occupation) {
Person.call(this, name, age);
this.occupation = occupation;
}
Worker.prototype = Object.create(Person.prototype);
Worker.prototype.selfPresentation = function() {
Person.prototype.selfPresentation.call(this); 
console.log("I don't breath only, I'm a " + this.occupation);
}
let variable = new Worker("Arturo", 22, "Student");
variable.selfPresentation();

最新更新