在ES6中返回除该类以外的值 [英] Return a value other than the class in ES6

查看:81
本文介绍了在ES6中返回除该类以外的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在使用ES6来测试课程,我注意到,创建一个类时,你不能指定构造函数给出的值。



ES5这是可能的。



在这两种情况下,我将使用新的MyClass
实例化该类我想这样做是这样的,所以我可以返回当前类的一个子集,只有它的功能。



ES5 - 返回我的类是init与:Blah



  var MyClass = function(){
this.initVar = Blah'

return'我的类是init与:'+ this.initVar
}



ES6 - 返回 {}



  class Bob {
constructor(){
return'hello'
}
}


解决方案

根据 文章 fr在TC39网站上,ES6类语法具有隐式构造函数,如果在类定义中没有提供此类函数,则会调用该函数。



这可以通过提供自己的构造函数和返回任何所需的对象来覆盖,例如:

  class Bob {
constructor(name){
return {hi:name}; //返回除* this *之外的对象。
}
}

在行动中:

  var bob = new Bob('bob'); 
console.log(bob.hi); //'bob'

要扩展课程,您可以执行以下操作:

  class Bill扩展Bob {
}

但是,扩展也有一个隐式的构造函数,所以它将返回一个继承自Bob.prototype 的新的 Bill 实例。 。由于 hi 在构造函数中被定义而不是继承,所以你得到:

  var bill = new Bill ('法案'); 
console.log(bill.hi); // undefined

要将Bill与 Bob 相同,请提供一个构造函数那个叫超级。此呼叫还将

  class Bill扩展Bob {
constructor(name){
super(name);
}
}

现在:

  var bill = new Bill('bill'); 
console.log(bill.hi); // bill

还值得注意的是,一个 classDeclaration 的正文总是严格模式代码



作为一个可运行的代码段:



 类Bob {constructor(name){return {hi:name}; //返回除* this *之外的对象。 }} var bob = new Bob('bob'); console.log(bob.hi); //'bob'class Bill扩展Bob {constructor(name){super(name); }} var bill = new Bill('bill'); console.log(bill.hi); // bill  


Recently I've been testing out classes with ES6, I've noticed that when creating a class you cannot specify the value given by the constructor.

Previously in ES5 this was possible.

In both cases I would instantiate the class with new MyClass The reason I want to do this is so I can return a subset of the current class with only functions on it.

ES5 - returns My class was init with: Blah

var MyClass = function() {
  this.initVar = 'Blah'

  return 'My Class was init with: ' + this.initVar
}

ES6 - returns {}

class Bob {
  constructor() {
   return 'hello' 
  }
}

解决方案

According to the Class article from the TC39 web site, the ES6 class syntax has an implicit constructor function that is called if no such function is provided in the class definition.

This can be overridden by providing your own constructor and returning whatever object you want, e.g.:

class Bob {
  constructor(name) {
    return {hi: name};  // returns an object other than *this*.
  }
}

In action:

var bob = new Bob('bob');
console.log(bob.hi); // 'bob'

To extend the class, you can do:

class Bill extends Bob {
}

However extends also has an implicit constructor, so it will return a new instance of Bill that inherits from Bob.prototype. Since hi was defined in the constructor and not inherited, you get:

var bill = new Bill('bill');
console.log(bill.hi);  // undefined

To initialise Bill the same as Bob, provide a constructor that calls super. This call also changes the this object of Bill to the value returned by super, e.g.

class Bill extends Bob {
  constructor(name) {
    super(name);
  }
}

Now:

var bill = new Bill('bill');
console.log(bill.hi); // bill

Also worth noting that the body of a classDeclaration is always strict mode code.

As a runnable snippet:

class Bob {
  constructor(name) {
    return {hi: name};  // returns an object other than *this*.
  }
}

var bob = new Bob('bob');
console.log(bob.hi); // 'bob'

class Bill extends Bob {
  constructor(name) {
    super(name);
  }
}

var bill = new Bill('bill');
console.log(bill.hi); // bill

这篇关于在ES6中返回除该类以外的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆