调用静态方法的标准方法是什么?我可以考虑使用constructor或使用类本身的名称,我不喜欢后者,因为它没有必要.前者是推荐的方式,还是还有其他的东西?
这是一个(人为的)例子:
class SomeObject {
  constructor(n){
    this.n = n;
  }
  static print(n){
    console.log(n);
  }
  printN(){
    this.constructor.print(this.n);
  }
}
如何使用ES6克隆Javascript类实例.
我对基于jquery或$ extend的解决方案不感兴趣.
我已经看到对对象克隆的相当古老的讨论表明这个问题非常复杂,但是使用ES6可以得到一个非常简单的解决方案 - 我会把它放在下面,看看人们是否认为它是令人满意的.
编辑:建议我的问题是重复的; 我看到了这个答案,但它已经有7年的历史,并且使用前ES6 js涉及非常复杂的答案.我建议我的问题,允许ES6,有一个非常简单的解决方案.
在ES6中添加的JavaScript的类语法显然使得扩展null是合法的:
class foo extends null {}
一些谷歌搜索显示在ES讨论中建议这样的声明是错误的; 然而,其他评论者认为他们在此基础上是合法的
有人可能想要创建一个具有
{__proto__: null}原型的类
争论的那一方最终占了上风.
我无法理解这个假设的用例.首先,虽然这样一个类的声明是合法的,但似乎实例化以这种方式声明的类不是.试图foo在Node.js或Chrome中从上面实例化类给我一个奇妙的愚蠢错误
TypeError: function is not a function
虽然在Firefox中做同样的事情给了我
TypeError: function () {
} is not a constructor
在类上定义构造函数没有帮助,如MDN当前的特性示例所示; 如果我尝试实例化这个类:
class bar extends null {
  constructor(){}
}
然后Chrome/Node告诉我:
ReferenceError: this is not defined
和Firefox告诉我:
ReferenceError: |this| used uninitialized in bar class constructor
所有这些疯狂是什么?为什么这些空扩展类不可实例化?鉴于它们不是可实例化的,为什么在规范中故意创建它们的可能性,为什么一些MDN作者认为它值得注意到足以记录?这个功能有哪些可能的用例?
作为一种组织模式,ES6类可以为异步代码提供什么.下面是ES7 async/await的示例,ES6类可以在ES7中使用异步方法或构造函数吗?
我可不可以做:
class Foo {
    async constructor() {
        let res = await getHTML();
        this.res = res
    }
}
而且,如果不是,构造函数应如何工作呢?
class Foo {
    constructor() {
        getHTML().then( function (res) {
            this.res = res
        }
    }
}
如果这些模式都不起作用,那么ES6中的构造函数(以及类)是否class支持对对象状态进行操作的任何形式的异步性?或者,它们仅用于纯粹的同步代码库吗?上面的例子在构造函数中,但它们不需要是......将问题再推下一级......
class Foo {
    myMethod () {
      /* Can I do anything async here */
    }
}
或者,用吸气剂......
class Foo {
    get myProp() {
        /* Is there any case that this is usefully asynchronous */
    }
}
我能想到的唯一例子是在同一个方法/构造函数/ getter中并行运行某些东西,但是要在结束之前解决整个问题.我只是感到困惑,因为它似乎完全异步库的所有推动,这只是混淆事情.除了教科书示例,我找不到一个他们有用的应用程序.
我看到使用ES6类使用单例模式的模式,我想知道为什么我会使用它们而不是仅仅在文件底部实例化类并导出实例.这样做有什么负面的缺点吗?例如:
ES6导出实例:
import Constants from '../constants';
class _API {
  constructor() {
    this.url = Constants.API_URL;
  }
  getCities() {
    return fetch(this.url, { method: 'get' })
      .then(response => response.json());
  }
}
const API = new _API();
export default API;
用法:
import API from './services/api-service'
使用以下Singleton模式有什么区别?是否有任何理由使用另一个?我真的更好奇,知道我给出的第一个例子是否会有我不知道的问题.
单身模式:
import Constants from '../constants';
let instance = null;
class API {
  constructor() {
    if(!instance){
      instance = this;
    }
    this.url = Constants.API_URL;
    return instance;
  }
  getCities() {
    return fetch(this.url, { method: 'get' })
      .then(response => response.json());
  }
} …我有以下 TypeScript 类
export class Vehicule extends TrackableEntity {
  vehiculeId: number;
  constructor() {
    super();
    return super.proxify(this);
  }
}
我在 tsconfig.json 中的打字稿目标配置为 es6:
"compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
}
在运行时,在 Chrome 中,代码失败:
ReferenceError: Cannot access 'Vehicule' before initialization
    at Module.Vehicule (https://localhost:44338/src-app-components-dispositifsDATI-dispositifsDATI-module.js:10559:100)
    at Module../src/app/domain/models/VehiculeGpsBoxInfo.ts (https://localhost:44338/src-app-components-dispositifsDATI-dispositifsDATI-module.js:11156:69)
    at __webpack_require__ (https://localhost:44338/runtime.js:84:30)
    at Module../src/app/domain/models/Vehicule.ts (https://localhost:44338/src-app-components-dispositifsDATI-dispositifsDATI-module.js:10571:78)
    at __webpack_require__ (https://localhost:44338/runtime.js:84:30)
    at Module../src/app/components/dispositifsDATI/mainDATI/listDATI/listDATI.component.ts (https://localhost:44338/src-app-components-dispositifsDATI-dispositifsDATI-module.js:6447:82)
    at __webpack_require__ (https://localhost:44338/runtime.js:84:30)
    at Module../src/app/components/dispositifsDATI/index.ts (https://localhost:44338/src-app-components-dispositifsDATI-dispositifsDATI-module.js:3053:95)
    at __webpack_require__ (https://localhost:44338/runtime.js:84:30)
    at Module../src/app/components/dispositifsDATI/dispositifsDATI.routes.ts (https://localhost:44338/src-app-components-dispositifsDATI-dispositifsDATI-module.js:2982:64)
我需要将 es5 更改为 es6 以解决另一个问题 …
在ECMAScript 6 typeof中,根据规范,类是'function'.
但是,根据规范,您不能将通过类语法创建的对象称为普通函数调用.换句话说,您必须使用new关键字否则会引发TypeError.
TypeError: Classes can’t be function-called
因此,如果不使用try catch,这将非常丑陋并破坏性能,您如何检查函数是来自class语法还是function语法?
我想在ES6类中有一个静态属性.此属性值最初是一个空数组.
    class Game{
        constructor(){
           // this.cards = [];
        }
        static cards = [];
    }
    
    Game.cards.push(1);
    console.log(Game.cards);我该怎么做?
我是反应路由器(v4)的新手,我正在按照教程直到这个小问题:
这段代码效果很好
class App extends Component {
render() {
return (
    <Router>
       <div>
           <ul>
              <li><Link to="/link/value1" className="nav-link">Value1</Link></li>
              <li><Link to="/link/value2" className="nav-link">Value2</Link></li>
              <li><Link to="/link/value3" className="nav-link">Value3</Link></li>
         </ul>
         <hr/>
         <Route path="/link/:id" component={Generic}/>
       </div>
    </Router>
);
}
}
我定义了一个到/ link /:id的路由,其中包含3个链接.然后我声明我的"通用"组件(作为一个函数):
const Generic = ({ match }) => (
<div className="wrapper">
    <div className="section">
        <div className="container">
            <h3>Menu : {match.params.id}</h3>
        </div>
    </div>
</div>
)
这项工作如预期,没问题.但是现在,我想用ES6类语法声明我的组件,如下所示:
class Generic extends React.Component {
constructor(props) {
    super(props);
}
render() {
    return (
        <div className="wrapper">
            <div className="section">
                <div className="container">
                    <h3>Menu : {this.props.params.id}</h3> …我有一个JavaScript ES6类,它具有一个属性集,set并通过get函数进行访问.它也是一个构造函数参数,因此可以使用所述属性实例化该类.
class MyClass {
  constructor(property) {
    this.property = property
  }
  set property(prop) {
  // Some validation etc.
  this._property = prop
  }
  get property() {
    return this._property
  }
}
我_property用来逃避使用get/set的JS问题,如果我直接设置它会导致无限循环property.
现在我需要对MyClass的一个实例进行字符串化,以便通过HTTP请求发送它.字符串化的JSON是一个对象,如:
{
   //...
   _property:
}
我需要保留生成的JSON字符串,property以便我发送给它的服务可以正确解析它.我还需要property保留在构造函数中,因为我需要从服务发送的JSON构造MyClass的实例(它发送property没有的对象_property).
我该如何解决这个问题?如果我只是把它发送到HTTP请求之前截获MyClass的实例,并发生变异_property,以property使用正则表达式?这看起来很难看,但我能够保留当前的代码.
或者,我可以拦截从服务发送到客户端的JSON,并使用完全不同的属性名称实例化MyClass.但是,这意味着服务两侧的类的不同表示.
es6-class ×10
javascript ×8
ecmascript-6 ×7
static ×2
angular ×1
asynchronous ×1
class ×1
es6-promise ×1
node.js ×1
stringify ×1
typescript ×1
webpack ×1