javascript - 通过原型扩展 JavaScript 中的对象功能
问题描述
我正在自学地学习 JavaScript,我正在寻找从对象继承并扩展它们的功能的方法......而且......哇!事实证明有几种方法可以做到这一点。
所以有一个流行的方法是通过extend 和class 关键字(简而言之)扩展一个对象,我在下面放了一个我看到的例子。好的,因为我来自C++和Python语言,这确实是!非常有帮助......但我知道这是帮助来自面向对象语言的程序员感觉 JavaScript 更舒适的糖代码。所以!我的目标是知道如何在不使用上述方法的情况下扩展对象功能,因为我渴望了解 js 如何在幕后工作(至少非常接近它)并对此感到满意。
笔记
我知道这里有关于这个主题的帖子,但我认为那些不符合我的需求,考虑到那些(非常好)深入研究的帖子是从 2012 年左右开始的。
用class和extend关键字方式
参考: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
语法的情况下进行继承更感兴趣。这是我将从您的示例中更改的内容:
- 不要为原型创建类的新实例。属性都存在于对象中,而不是原型中。相反,使用
Object.create()
类的原型创建一个新对象。有关此问题的更多信息,请参阅此答案。 - 永远不要使用
__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();
推荐阅读
- android - 无法解析 Android Studio 中的依赖项
- php - 在 accountExpire AD 属性中转换当前日期 + 1 个月
- oracle - symmetricds 3.10.4 版开源工具安装和设置
- javascript - 有没有办法在 es 模块中导出 Promise 的结果?
- vue.js - VueJS 无法在不修复 linting 错误的情况下构建项目
- python - 在 Python 中查找 2D np.array 中的局部最小值
- regex - Mongo RegEx - 匹配所有类型的空格字符
- sctp - SCTP_PRIMARY_ADDR 和 SCTP_SET_PEER_PRIMARY_ADDR 选项的 Setsockopt 问题
- maven - 运行maven依赖时如何排除项目内依赖:go-offline
- angular - 我需要将 mat-button-toogle 更改为单选按钮?