我编写的代码看起来像:
function getStuffDone(param) { | function getStuffDone(param) {
var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) {
// or = new $.Deferred() etc. | // using a promise constructor
myPromiseFn(param+1) | myPromiseFn(param+1)
.then(function(val) { /* or .done */ | .then(function(val) {
d.resolve(val); | resolve(val);
}).catch(function(err) { /* .fail */ | }).catch(function(err) {
d.reject(err); | reject(err);
}); | });
return d.promise; /* or promise() */ | });
} | }
Run Code Online (Sandbox Code Playgroud)
有人告诉我这个被称为" 延迟反模式 "或" Promise构造函数反模式 ",这个代码有什么不好,为什么这被称为 …
我正在使用ECMAScript6模块.从以下选项中导出/导入模块中的多个方法的正确方法是什么?
单类静态方法:
//------ myClass.js ------
export default class myClass {
static myMethod1() {
console.log('foo');
}
static myMethod2(args...) {
console.log('bar');
}
}
//------ app.js ------
import myClass from 'myClass';
myClass.myMethod1(); //foo
Run Code Online (Sandbox Code Playgroud)
多种导出方法:
//------ myMethods.js ------
export function myMethod1() {
console.log('foo');
}
export function myMethod2() {
console.log('bar');
}
//------ app.js ------
import {myMethod1, myMethod2} from 'myMethods';
myMethod1() //foo;
//OR
import * as myMethods from 'myMethods';
myMethods.myMethod1() //foo;
Run Code Online (Sandbox Code Playgroud)
1)导出:一类只是静态方法感觉有点"代码味道",但同样单独导出所有内容确实感觉有点冗长.它只是开发人员偏好还是存在性能影响?
2)导入:'*as'语法是我首选的方法,因为它允许您使用点符号(引用模块和方法)帮助代码可读性.当我可能只使用其中一种方法时,这是否有性能影响?
我很好奇,因为我得到"未定义不是函数"错误.考虑以下课程:
var FlareError = require('../flare_error.js');
class Currency {
constructor() {
this._currencyStore = [];
}
static store(currency) {
for (var key in currency) {
if (currency.hasOwnProperty(key) && currency[key] !== "") {
if (Object.keys(JSON.parse(currency[key])).length > 0) {
var currencyObject = JSON.parse(currency[key]);
this.currencyValidator(currencyObject);
currencyObject["current_amount"] = 0;
this._currencyStore.push(currencyObject);
}
}
}
}
currencyValidator(currencyJson) {
if (!currencyJson.hasOwnProperty('name')) {
FlareError.error('Currency must have a name attribute in the json.');
}
if (!currencyJson.hasOwnProperty('description')) {
FlareError.error('Currency must have a description attribute in the json.');
}
if (!currencyJson.hasOwnProperty('icon')) {
FlareError.error('Currency …Run Code Online (Sandbox Code Playgroud)