【发布时间】:2019-07-07 23:55:46
【问题描述】:
我试图了解当我们在 ES6 中使用“类”时所有语法糖下发生了什么,以及当我们使用“new”和“this”关键字时发生了什么。我知道 JavaScript 是一种基于原型的编程语言,我想出了下面的代码来做到这一点,而不使用“new”或“this”。
//Constructor function passes 3 custom properties to a predetermined object and then returns that object.
function ConstructorTestFunction(value1, value2, value3, object){
object.property1 = value1;
object.property2 = value2;
object.property3 = value3;
return object;
}
/*Since I am not using "this" I have wrapped the "ConstructorTestFunction.prototype" object into a function
called "ConstructorTestFunctionPrototype". This will allow me to pass the parent object's properties into
methods within the objects ".prototype" property */
function ConstructorTestFunctionPrototype(object){
ConstructorTestFunction.prototype = {
addValues(){
return object.property1 + object.property2 + object.property3;
},
minusValues(){
return object.property1 - object.property2 - object.property3;
},
multiplyValues(){
return object.property1 * object.property2 * object.property3;
},
divideValues(){
return object.property1 / object.property2 / object.property3;
}
};
return ConstructorTestFunction.prototype
};
// Since I am not using "new", I set _object1 to a blank javascript object with [[prototype]] = Object.prototype
const _object1 = {};
/*Change _object1's [[prototype]] to "ConstructorTestFunction.prototype", with the object's parent object
passed into it's hidden [[prototype]] property*/
_object1.__proto__ = ConstructorTestFunctionPrototype(_object1);
/*Creates object1, which is an instance of ConstructorTestFunction. It passes in "_object1" which is an object
with "[[prototype]] = ConstructorTestFunctionPrototype(_object1)", so it inherits all the methods from
"ConstructorTestFunction.prototype" whilst referencing the properties of the parent object. It also passes in
values for all assigned properties included in the class.
*/
const object1 = ConstructorTestFunction(40, 50, 245, _object1);
// Since I am not using "new", I set _object2 to a blank javascript object with [[prototype]] = Object.prototype
const _object2 = {};
/*Change _object2's [[prototype]] to "ConstructorTestFunction.prototype", with the object's parent object
passed into it's hidden [[prototype]] property*/
_object2.__proto__ = ConstructorTestFunctionPrototype(_object2);
/*Creates object2, which is an instance of ConstructorTestFunction. It passes in "_object2" which is an object
with "[[prototype]] = ConstructorTestFunctionPrototype(_object2)", so it inherits all the methods from
"ConstructorTestFunction.prototype" whilst referencing the properties of the parent object. It also passes in
values for all assigned properties included in the class.
*/
const object2 = ConstructorTestFunction(1, 2, 3, _object2);
除此之外,这不是 Javascript 的标准处理方式。我重新编写代码以不使用“this”或“new”的这种方法是否会产生重大的性能问题?如果是这样,那么我将如何在没有重大性能问题的情况下替换“this”和“new”?
基本上,如果我真正了解 [[prototype]] 以及“new”和“this”在做什么,那么我应该能够创建一些使用 [[prototype]] 在 JavaScript 中创建“类”的代码不使用“new”和“this”。否则,如果我不能在自定义代码中替换它们的功能,我真的不知道这些关键字在做什么。
提前感谢您的帮助。
【问题讨论】:
-
在像 JavaScript 这样的任何语言中,运行时代码可以做的事情在语言本身内实际上是做不到的。
-
如果您能详细说明您不想使用
new和this的原因,也会有所帮助。 -
你所做的不是类的工作方式......至少如果不使用
this,就无法重写类完成的工作。如果你改用Object.create(),你可以避免使用new,但这样做似乎很愚蠢。
标签: javascript class this prototype new-operator