[英]Compiling ES6 to ES5 constructor in loose mode
In the Exploring ES6 book, I was reading about how constructors are compiled to ES5 in loose mode. 在《 探索ES6》一书中,我正在阅读有关如何在松散模式下将构造函数编译为ES5的内容。 One example is this:
一个例子是这样的:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
Is compiled into this: 编译成这样:
"use strict";
function _classCallCheck(instance, Constructor) {
if(!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var Point = (function () {
function Point(x, y) {
_classCallCheck(this, Point);
this.x = x;
this.y = y;
}
Point.prototype.toString = function toString() { // (A)
return "(" + this.x + ", " + this.y + ")";
};
return Point;
})();
I don't understand this line: 我不明白这一行:
_classCallCheck(this, Point);
So what does Point
actually mean here? 那么
Point
在这里实际上意味着什么? Does it refer to the function Point
? 它是否指向
function Point
? In which case, of course this
is an instance of Point
because it also refers to function Point
, so _classCallCheck
will always return true
. 在这种情况下,
this
当然是Point
的实例,因为它也引用了function Point
,因此_classCallCheck
将始终返回true
。
So what does Point actually mean here?
那么Point在这里实际上意味着什么? Does it refer to the function Point?
它是否指向功能Point?
Yes 是
What _classCallCheck
is doing is checking to see if a new instance of the class Point was created. _classCallCheck
所做的是检查是否已创建Point类的新实例。 It prevents someone from doing the following: 它阻止某人执行以下操作:
var test = Point(); // THROWS ERROR
In the previous snippet example, _classCallCheck(this, Point)
, this
will be whatever the outer scope of this code is (probably window). 在前面的片段示例
_classCallCheck(this, Point)
, this
将是此代码的外部范围(可能是窗口)的任何范围。
It forces you to instantiate a new instance: 它迫使您实例化一个新实例:
var test = new Point(); // VALID
So what does Point actually mean here?
那么Point在这里实际上意味着什么?
It is the Point
function, which is stored in var Point
. 这是
Point
函数,存储在var Point
。
In which case, of course this is an instance of Pointbecause it also refers to function Point, so _classCallCheck will always return true.
在这种情况下,这当然是Point的实例,因为它也引用了Point函数,因此_classCallCheck将始终返回true。
Not so. 不是这样 It's easy to call
Point
in such a way that this
is not an instance of Point
. 这很容易调用
Point
以这样一种方式, this
不是一个实例Point
。 ES6 classes, by their nature, prevent calling class names in this way, but ES5 does not, so the _classCallCheck
is there to ensure ES6 behavior is preserved. ES6类就其本质而言可以防止以这种方式调用类名,但是ES5不能,因此
_classCallCheck
可以确保保留ES6行为。 Observe: 注意:
"use strict"; function _classCallCheck(instance, Constructor) { if(!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Point = (function () { function Point(x, y) { _classCallCheck(this, Point); this.x = x; this.y = y; } Point.prototype.toString = function toString() { // (A) return "(" + this.x + ", " + this.y + ")"; }; return Point; })(); // Invalid call prevented by _classCallCheck Point(1, 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.