我看到使用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) 这是一个惯例问题.我是ES6的新手,但我正在尝试使用模块系统.从单个文件导出多个函数或导出包含这些函数的单个对象是首选/更常见的.
例:
utils.js
export function add(num1, num2) {
return num1 + num2;
}
export function minus(num1, num2) {
return num1 - num2;
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
import {add, minus} from 'utils.js';
Run Code Online (Sandbox Code Playgroud)
VS
utils.js
const utils = {
add: (num1, num2) => {
return num1 + num2;
},
minus: (num1, num2) => {
return num1 - num2;
}
}
export default utils;
Run Code Online (Sandbox Code Playgroud)
在包含50-100个函数的utils文件中,第二种方式似乎是明显的赢家.但是对我来说只有一些感觉不对的东西,我不知道为什么.
建立:
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:在构造函数中初始化状态的能力有哪些好处?
所以基本上我不确定何时使用哪个实用程序类,并希望您的输入.
我已经找到了至少两种从像Ramda这样的模块导入函数的方法.可能还有一些方法可以做一些非常相似的事情const R = require('ramda');
选项1是导入某些功能:
import { cond, T, always, curry, compose } from 'ramda';
Run Code Online (Sandbox Code Playgroud)
选项2是导入整个模块,如:
import * as R from "ramda";
Run Code Online (Sandbox Code Playgroud)
我更喜欢引用调用函数的模块,如下所示:
R.T();
Run Code Online (Sandbox Code Playgroud)
但是如果使用第二个选项,它是否会引入每个Ramda函数,而不仅仅是我正在使用的模块中使用的函数?如果使用选项2,是否会对实际内存使用或带宽使用产生影响?有可能以某种方式这样做:
// invalid syntax below:
import R { cond, T, always, curry, compose } from 'ramda';
R.T();
Run Code Online (Sandbox Code Playgroud)
我的问题与此问题有点相关,但它有点不同 导入R(ramda)到typescript .ts文件
javascript bundling-and-minification ecmascript-6 es6-modules
我有一个名为的模块fooModule.在这个模块中,我导入fooModule(本身):
import * as fooModule from './fooModule';
export function logFoo() {
console.log(fooModule)
}
Run Code Online (Sandbox Code Playgroud)
当logFoo()被调用时,我可以看到所有的fooModule的出口.这是如何运作的?
我正在尝试创建一个带有静态函数的"utils"类,用于我在本机中工作的项目.
我读到了如何在StackOverFlow 问题中创建静态函数,但由于某些奇怪的原因,它对我不起作用.
//utils.js
'use strict'
export default {
textFormat(args) {
var s = args[0];
for (var i = 0; i < args.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, args[i + 1]);
}
return s;
}
}
//main file
import * as Utils from './utils/utils';
/**
/...
**/
var text = Utils.textFormat(rowData.text, "<Text style={{color:'blue'}}>Hey there</Text>");
Run Code Online (Sandbox Code Playgroud)
但我一直得到这个错误'Utils.textFormat不是一个函数',我做错了什么?
我一直在尝试对此进行研究,但找不到任何建议。我继承了一个代码库,其中团队使用带有静态方法的类,而不是辅助方法的函数。我从未见过采用这种方法并试图决定是否应该让它们返回并使用它们创建函数。我觉得这很不干净,因为您要导入整个类而不只是要使用的功能,所以导入很膨胀?
一种方法比另一种更好吗?
例如,在我不清楚的情况下:
export class StringUtil {
public static alterString(str: string) {
return alteredString;
}
}
Run Code Online (Sandbox Code Playgroud)
与
export function alterString(str: string) {
return alteredString;
}
Run Code Online (Sandbox Code Playgroud)
然后将这样使用:
import { StringUtil } from '../StringUtil';
getString(str: string) {
return StringUtil.alterString(str);
}
Run Code Online (Sandbox Code Playgroud)
与
import { alterString } from '../helper-functions';
getString(str: string) {
return alterString(str);
}
Run Code Online (Sandbox Code Playgroud) 我无法从类中的静态方法调用私有或非静态方法,下面是示例
class a {
fun1(){
console.log('fun1');
}
static staticfun(){
console.log('staticfun');
this.fun1();
}
}
a.staticfun();
Run Code Online (Sandbox Code Playgroud)
我试图仅公开 staticfun 方法,该方法在内部调用所有私有方法,但这给我的this.fun1不是一个函数。我试图找到很多方法来用“this”找到它,但它确实有效。
如何在静态方法中调用私有实例方法?
我有一个 JavaScript 文件,其中包含以下内容:
const api = {
call(type, url, data) {
console.log(type + "bar");
},
get(url, query) {
this.call("foo");
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够api.get()在必要时跨多个不同的文件进行调用,但我在导入时遇到问题。
import当我尝试访问变量时,单独加载文件会给我一个 ReferenceError api:
import "services/api.js";
console.log(api);
Run Code Online (Sandbox Code Playgroud)
未捕获的引用错误:api 未定义
给导入一个名称(of api)会返回一个对象,但它没有内部方法:
import api from "../../services/api.js";
console.log(api);
console.log(api.get);
Run Code Online (Sandbox Code Playgroud)
对象 {}
未定义
我究竟做错了什么?
基本代码main.js:
class Something {
static ThisIsStaticFunction () { ... }
}
export default Something;
Run Code Online (Sandbox Code Playgroud)
其他文件xxx.js:
import xxx;
// I want to call `ThisIsStaticFunction()` without having to
// write `Something.ThisIsStaticFunction()` how can I achieve this?
Run Code Online (Sandbox Code Playgroud)
我想打电话ThisIsStaticFunction()而不必写Something.ThisIsStaticFunction()我怎样才能做到这一点?
我有一个FooService通过import-map加载的 Singleton-Class 。我想(a)等待它并在各种异步函数中使用它,如下所示:
declare global {
interface Window {
System: System.Module
}
}
const module = window.System.import('@internal/foo-service')
const fooService = module.FooService
async function func1() {
await fooService.doBar()
.
.
}
async function func2() {
await fooService.doBar2()
.
.
}
Run Code Online (Sandbox Code Playgroud)
但我只能让它像这样工作:
declare global {
interface Window {
System: System.Module
}
}
async function getfooService() {
const module = await window.System.import('@internal/foo-service')
return module.FooService
}
function func1() {
getfooService().then(fooService => fooService .doBar())
.
.
}
function func2() {
getfooService().then(fooService …Run Code Online (Sandbox Code Playgroud) 我有两个文件:User.js和Login.js.登录成功后我想调用logInUser类的静态方法.我有奇怪的行为.我究竟做错了什么?
文件内容User.js:
// user/User.js
// I also tried export default class User
export class User {
static logIn (token) {
}
static logOut (token) {
}
static isAuthorized () {
}
}
Run Code Online (Sandbox Code Playgroud)
和Login.js:
// login/Login.js
import React from 'react';
import GoogleLogin from 'react-google-login';
// I also tried import User from './../user/User';
// I also tried import {User} from './../user/User';
import * as User from './../user/User';
class Login extends React.Component { …Run Code Online (Sandbox Code Playgroud) javascript ×10
ecmascript-6 ×8
babeljs ×3
es6-modules ×2
reactjs ×2
typescript ×2
commonjs ×1
es6-class ×1
import-maps ×1
jquery ×1
oop ×1
static ×1
system.js ×1
webpack ×1