在JavaScript中实现单例模式的最简单/最简洁的方法是什么?
似乎可以在构造函数中嵌套一个类,然后可以从类中的任何地方实例化,这是官方的吗?
[编辑]例如,
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) 我看到使用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;
Run Code Online (Sandbox Code Playgroud)
用法:
import API from './services/api-service'
Run Code Online (Sandbox Code Playgroud)
使用以下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());
}
} …Run Code Online (Sandbox Code Playgroud) 我只是想更深入地理解Javascript.
我创建了一个'类' gameData,我只想要一个,不需要构造函数,或者实例化.
所以我就这样创造了......
var gameData = new function () {
//May need this later
this.init = function () {
};
this.storageAvailable = function () {
if (typeof (Storage) !== "undefined") {
return true;
}
else {
return false;
}
};
}
Run Code Online (Sandbox Code Playgroud)
意识到'new'关键字不允许它实例化并使其可用LIKE静态类将在C#中.
我是否正确地想到了这一点?静电?
建立:
CommonJS和ES6的新功能.我知道对象实例和方法的静态容器之间的区别,但我不确定它们在分离到模块时的行为方式.所以我想知道返回一个实例之间的区别是什么(这个模式是否有效?):
// StateParser.js
class StateParser {
constructor() {
}
method1() {
...
}
}
export default new StateParser()
Run Code Online (Sandbox Code Playgroud)
并导出const方法:
// StateParser.js
let state = {
}
export const method1 = () => { ... }
Run Code Online (Sandbox Code Playgroud)
方法B:使用对象解构的能力之一是:
import { method1 } from '../utils/StateParser.js';
Run Code Online (Sandbox Code Playgroud)
然后使用method1,就好像它存在于本地?
方法A:在构造函数中初始化状态的能力有哪些好处?
所以基本上我不确定何时使用哪个实用程序类,并希望您的输入.
通常我会像这样应用装饰器:
class SpecialMethods {
@Deco
static someMethod() {
}
}
Run Code Online (Sandbox Code Playgroud)
是否还有一些方法可以将它与普通对象而不是类一起使用:
const SpecialMethods = {
@Deco
someMethod: () => {}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法初始化对象文字并同时声明其具有只读属性的接口?
例如
let a = { readonly b: 2, readonly c: 3 }
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 JS ES6 类中实现单例模式。这是我到目前为止所写的:
let instance;
export class TestClass{
constructor(){
if(new.target){
throw new Error(`Can't create instance of singleton class with new keyword. Use getInstance() static method instead`);
}
}
testMethod(){
console.log('test');
}
static getInstance(){
if(!instance) {
instance = TestClass.constructor();
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我调用静态方法时TestClass.getInstance(),我没有得到类对象的实例,我得到了
ƒ anonymous() {
}
Run Code Online (Sandbox Code Playgroud)
函数,无需访问 testMethod。我在我的代码中找不到错误 - 将不胜感激。
我在Babel环境中的ES6中遇到了这个问题:
// A.js
class A {
}
export default new A();
// B.js
import C from './C';
class B {
}
export default new B();
// C.js
import A from './A';
import B from './B';
class C {
constructor(A, B){
this.A = A;
this.B = B; // undefined
}
}
export default new C(A, B)
Run Code Online (Sandbox Code Playgroud)
我像这样导入它们:
// stores/index.js
import A from './A';
import B from './B';
import C from './C';
export {
A,
B,
C
}
Run Code Online (Sandbox Code Playgroud)
从我的应用程序入口点我做:
import * as …Run Code Online (Sandbox Code Playgroud) javascript module circular-dependency ecmascript-6 es6-modules
模块/IIFE 等有点麻烦。我有一个脚本,它曾经是一个 IIFE,并且使用了很多这个关键字等等。我正试图将它变成一个模块。
我有以下模块dice.js:
export default function () {
this.createDice = function() { ... }
...
}
Run Code Online (Sandbox Code Playgroud)
在主应用程序上,我称之为:
import Dice from "./dice.js";
let DICE = new Dice();
let dice1 = DICE.createDice();
let dice2 = DICE.createDice();
Run Code Online (Sandbox Code Playgroud)
它有效......我的问题是,有没有办法避免创建额外的 DICE 变量来调用所有方法?换句话说,我想这样称呼它:
import Dice from "./dice.js";
let dice1 = Dice.createDice();
let dice2 = Dice.createDice();
Run Code Online (Sandbox Code Playgroud)
我已经尝试过 IIFE,但无法解决问题。
javascript ×8
ecmascript-6 ×4
es6-class ×2
es6-modules ×2
singleton ×2
typescript ×2
babeljs ×1
commonjs ×1
constructor ×1
decorator ×1
function ×1
iife ×1
module ×1
reactjs ×1
traceur ×1