标签: traceur

获取ES6类实例的类名

是否有任何"和谐"方法从ES6类实例中获取类名?以外

someClassInstance.constructor.name
Run Code Online (Sandbox Code Playgroud)

目前我指望Traceur的实施.似乎Babel有一个Function.namepolyfill ,而Traceur没有.

总而言之:ES6/ES2015/Harmony中没有其他方式,ES中没有预期的ATM.接下来.

它可以为未经管理的服务器端应用程序提供有用的模式,但在用于浏览器/桌面/移动设备的应用程序中是不需要的.

巴别使用core-js到填充工具Function.name,应当手动加载Traceur和打字稿应用适当.

javascript ecmascript-6 traceur

142
推荐指数
3
解决办法
8万
查看次数

如何用Traceur在ES6类中实现私有方法

我现在使用Traceur Compiler来获得ES6功能.

我想从ES5实现这些东西:

function Animal() {
    var self = this,
        sayHi;

    sayHi  = function() {
        self.hi();
    };

    this.hi = function() {/* ... */}
}
Run Code Online (Sandbox Code Playgroud)

目前traceur不支持privatepublic关键字(来自和谐).ES6类语法不允许在类体中使用简单var(或let)语句.

我找到的唯一方法是在类声明之前模拟私有.就像是:

var sayHi = function() {
    // ... do stuff
};

class Animal {
...
Run Code Online (Sandbox Code Playgroud)

没有什么比通过预期的更好,this没有apply-ing或bind-ing它每次都不能将正确的方法传递给私有方法.

那么,是否有可能在ES6类中使用与traceur编译器兼容的私有数据?

javascript class ecmascript-5 ecmascript-6 traceur

134
推荐指数
8
解决办法
19万
查看次数

如何使用ES6模块模拟单元测试的依赖关系

我正在尝试使用webpack + traceur来调用Ecmascript 6模块来转换为ES5 CommonJS,但是我无法成功地对它们进行单元测试.

我尝试使用Jest + traceur预处理器,但自动插件和依赖名称似乎变得棘手,而且我似乎无法使用sourceMaps来使用Jest和node-inspector调试.

是否有更好的单元测试ES6模块框架?

javascript unit-testing ecmascript-6 traceur jestjs

56
推荐指数
4
解决办法
2万
查看次数

ECMAScript 6:什么是WeakSet?

WeakSet应该通过弱引用来存储元素.也就是说,如果某个对象未被其他任何东西引用,则应该从WeakSet中清除它.

我写了以下测试:

var weakset = new WeakSet(),
    numbers = [1, 2, 3];

weakset.add(numbers);
weakset.add({name: "Charlie"});

console.log(weakset);

numbers = undefined;

console.log(weakset);
Run Code Online (Sandbox Code Playgroud)

即使我的[1, 2, 3]数组没有被任何引用引用,它也不会从WeakSet中删除.控制台打印:

WeakSet {[1, 2, 3], Object {name: "Charlie"}}
WeakSet {[1, 2, 3], Object {name: "Charlie"}}
Run Code Online (Sandbox Code Playgroud)

这是为什么?

另外,我还有一个问题.将对象直接添加到WeakSets有什么意义,如下所示:

weakset.add({name: "Charlie"});
Run Code Online (Sandbox Code Playgroud)

那些Traceur的故障还是我错过了什么?

最后,如果我们甚至无法迭代它并获得当前大小,那么WeakSet的实际用途是什么?

javascript ecmascript-6 traceur

52
推荐指数
4
解决办法
1万
查看次数

嵌套的ES6课程?

似乎可以在构造函数中嵌套一个类,然后可以从类中的任何地方实例化,这是官方的吗?

[编辑]例如,

class C {

    constructor() {
        class D {
            constructor() { }
        }
    }

    method() {
        var a = new D();  // works fine
    }

}

//var a = new D();  // fails in outer scope
Run Code Online (Sandbox Code Playgroud)

traceur生成了JS https://google.github.io/traceur-compiler/demo/repl.html

$traceurRuntime.ModuleStore.getAnonymousModule(function() {
  "use strict";
  var C = function C() {
    var D = function D() {};
    ($traceurRuntime.createClass)(D, {}, {});
  };
  ($traceurRuntime.createClass)(C, {method: function() {
      var a = new D();
    }}, {});
  return {};
});
//# sourceURL=traceured.js
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 traceur

50
推荐指数
3
解决办法
4万
查看次数

扩展ES6中的承诺

我想扩展Promise:

class PersistedPromise extends Promise { }
Run Code Online (Sandbox Code Playgroud)

然后resolve在派生类上调用static 以直接创建已解析的promise:

PersistedPromise.resolve(1)
Run Code Online (Sandbox Code Playgroud)

在traceur中,这会产生:

ModuleEvaluationError: #<PersistedPromise> is not a promise
    at new PersistedPromise (~rtm/gen/promise.js:6:57)
    at Function.resolve (native)
Run Code Online (Sandbox Code Playgroud)

在Babel(运行babel-node --experimental promise.js)中,它会导致:

    Promise.apply(this, arguments);
            ^
TypeError: [object Object] is not a promise
    at new PersistedPromise (~rtm/gen/promise.js:1:23)
    at Function.resolve (native)
    ...
Run Code Online (Sandbox Code Playgroud)

我依赖于此:

Promise的所有静态方法都支持子类化:它们通过接收器创建新实例(想想:new this(...))并通过它访问其他静态方法(this.resolve(...)与Promise.resolve(.. )).

来自http://www.2ality.com/2014/10/es6-promises-api.html.

似乎节点检查thison调用,例如Promise.resolve.call(this, val)是a Promise,而不是(正确?)Promise 或其派生类(v0.12.0).

以上是不再有效,或者没有制定规范,或者只是没有由traceur和/或节点实现?

promise ecmascript-6 traceur es6-promise babeljs

22
推荐指数
1
解决办法
4839
查看次数

覆盖setter,并且还必须覆盖getter

class AbstractClass {

    constructor() {
    }

    set property(value) {
        this.property_ = value;
    }

    get property() {
        return this.property_;
    }

}

class Subclass extends AbstractClass {

    constructor() {
        super();
    }

    set property(value) {
        super.property = value;
        if (!(this.property_ instanceof SubclassAssociatedClass)) throw new TypeError();
    }

    //get property() {
    //  return super.property;
    //}

}
Run Code Online (Sandbox Code Playgroud)

覆盖set属性的方法,看起来get方法也必须被覆盖,否则undefined返回(即,get方法不是继承的,取消注释get property()上面的子类方法,一切正常).

我认为这是规范的一部分,但如果行为是交叉编译的结果,可能会遵循.可以肯定的是,这是编码重写的setter和getter(同时还是根本没有)的正确方法?

javascript class getter-setter ecmascript-6 traceur

19
推荐指数
1
解决办法
1万
查看次数

我应该使用Traceur而不是Typescript来定位ES5,但是要为ES6做好准备

我正在开发一个可以从Typescript中受益的大型代码库,但是由于最终世界将转向ES6,我应该将开发引向Traceur吗?

我不想改变Typescript实现以定位ES6(准备就绪时),所以我现在的感觉是继续使用Traceur.

任何人都可以建议吗?

typescript traceur

18
推荐指数
3
解决办法
8881
查看次数

使用ES6类扩展数组

我听说ES6现在最终允许子类化Array.这是一个给出的例子

class Stack extends Array {
    constructor() { super() }
    top() { return this[this.length - 1]; }
  }

  var s = new Stack();
  s.push("world");
  s.push("hello");
  console.log(s.top());  // "hello"
  console.log(s.length); // 2
Run Code Online (Sandbox Code Playgroud)

当然,这很有效.但至少在Traceur中,明确设置长度并不会截断数组.当通过console.log打印时,输出是对象形式而不是数组形式,这表明某人并没有将其视为"真正的"数组.

这是Traceur如何实现内置对象的子类化或ES6限制的问题吗?

ecmascript-6 traceur

18
推荐指数
1
解决办法
1万
查看次数

angularjs http拦截器类(ES6)失去对'this'的绑定

我正在使用ES6类构建AngularJS应用程序,并将跟踪器转换为AMD格式的ES5.

在我的模块中,我导入拦截器类并将其注册为服务,然后使用module.config中的$ httpProvider.interceptors注册此服务:

var commonModule = angular.module(moduleName, [constants.name]);

import authenticationInterceptor from './authentication/authentication.interceptor';

commonModule.service('authenticationInterceptor', authenticationInterceptor);

commonModule.config( $httpProvider =>  {
    $httpProvider.interceptors.push('authenticationInterceptor');
});
Run Code Online (Sandbox Code Playgroud)

我的拦截器类注入$ q$ window服务,将它们保存在构造函数中供以后使用.我使用调试器跟踪了这一部分并正确地进行了注入:

'use strict';
/*jshint esnext: true */

var authenticationInterceptor = class AuthenticationInterceptor {

    /* ngInject */
    constructor($q, $window) {
        this.$q = $q;
        this.$window = $window;
    }

    responseError(rejection) {
        var authToken = rejection.config.headers.Authorization;
        if (rejection.status === 401 && !authToken) {
            let authentication_url = rejection.data.errors[0].data.authenticationUrl;
            this.$window.location.replace(authentication_url);
            return this.$q.defer(rejection);
        }
        return this.$q.reject(rejections);
    }
}

authenticationInterceptor.$inject = ['$q', …
Run Code Online (Sandbox Code Playgroud)

interceptor angularjs ecmascript-6 traceur gulp-traceur

16
推荐指数
2
解决办法
5410
查看次数