ES6允许扩展特殊对象.所以可以从函数继承.这样的对象可以作为函数调用,但是如何实现这种调用的逻辑呢?
class Smth extends Function {
constructor (x) {
// What should be done here
super();
}
}
(new Smth(256))() // to get 256 at this call?
Run Code Online (Sandbox Code Playgroud)
类的任何方法都可以通过引用类实例this.但是当它被称为函数时,this指的是window.当作为函数调用时,如何获取对类实例的引用?
PS:俄语同样的问题.
javascript inheritance function ecmascript-6 javascript-inheritance
我正在玩Typescript并试图理解编译器生成的已编译的Javascript代码
打字稿代码:
class A { }
class B extends A { }
Run Code Online (Sandbox Code Playgroud)
生成的Javascript代码:
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null …Run Code Online (Sandbox Code Playgroud) javascript prototype javascript-objects proto javascript-inheritance
考虑以下ES6类:
'use strict';
class Dummy {
}
class ExtendDummy extends Dummy {
constructor(...args) {
super(...args)
}
}
class ExtendString extends String {
constructor(...args) {
super(...args)
}
}
const ed = new ExtendDummy('dummy');
const es = new ExtendString('string');
console.log(ed instanceof ExtendDummy);
console.log(es instanceof ExtendString);Run Code Online (Sandbox Code Playgroud)
我的理解是两者都应该是true,而且在Firefox和Chrome中它们都是,但是Node说的es instanceof ExtendString是false.它与其他构造函数相同,而不仅仅是String.
我使用的软件:
--harmony标志的节点v5.11.0 .哪个JavaScript引擎是正确的,为什么?