我有代码:
// lib.js
var a = "a";
export var b = "b";
// main.js
console.log(a); // "a" variable is not available in a global scope
import {b} from "lib";
console.log(a); // is "a" variable available in a global scope or only in a module scope?
Run Code Online (Sandbox Code Playgroud)
我可以在模块导入后在全局范围内使用"a"变量,还是仅在模块范围内可用?ES6模块是否具有类似这样的工作原理:
// module
exports.module1 = (function(){ var a = "a"; })(); // "a" variable is not available in a global scope
Run Code Online (Sandbox Code Playgroud) #1
你好.我有代码:
class Component extends React.Component
{
render()
{
this.props.nested.prop = this.props.parse.nested.prop;
return <div>Component</div>;
}
componentDidMount()
{
console.log(this.props.nested.prop);
}
}
Component.defaultProps =
{
nested:
{
prop: "default"
}
};
const obj1 =
{
nested:
{
prop: "obj1"
}
};
const obj2 =
{
nested:
{
prop: "obj2"
}
};
class Application extends React.Component
{
render()
{
return (
<div>
<Component parse={obj1}/>
<Component parse={obj2}/>
</div>
);
}
}
React.render(<Application />, document.getElementById('app'));
//console output:
//"obj2"
//"obj2"
Run Code Online (Sandbox Code Playgroud)
为什么我为每个组件获得2个独立组件的1个变量引用而不是2个nested的nested.prop?为什么this.props在安装后只保存组件的所有实例的最后设置值?这是正常的行为吗?我认为正确的行为是针对不同的实例具有不同的属性值.
PS我在这里测试了这段代码.
#2
如何在没有迭代的情况下获取ES6 Map/Set的最后一个元素(forEach或for for)通过容器的整个长度?
我无法正确加载gulp babel/polyfill.在我的情况下,Array.from方法是未定义的.但是,如果尝试browser-polyfill.js使用gulp 加载,.add(require.resolve("babel/polyfill"))我会收到错误"only one instance of babel/polyfill is allowed".源代码是正确的,因为我用babel测试了它browser-polyfill.js.
源代码:
//Lib.js
export default class Lib
{
constructor()
{
var src = [1, 2, 3];
this.dst = Array.from(src);
}
foo()
{
return this.dst;
}
}
//main.js
import Lib from "./Lib";
var l = new Lib();
console.log(l.foo()); //Uncaught TypeError: Array.from is not a function
Run Code Online (Sandbox Code Playgroud)
Gulpfile:
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer'); …Run Code Online (Sandbox Code Playgroud) 我无法获取自定义画布元素的绘图上下文。
var customCanvas = Object.create(HTMLCanvasElement.prototype),
canvasConstructor = document.registerElement("custom-canvas", { prototype: customCanvas }),
canvas = document.createElement("custom-canvas"),
ctx = canvas.getContext("2d"); // Uncaught TypeError: Illegal invocation
Run Code Online (Sandbox Code Playgroud)
是错误、遗漏还是其他原因?
PS 我正在寻找仅基于 chromium 的浏览器的解决方案。
javascript ×4
ecmascript-6 ×3
babeljs ×1
browserify ×1
canvas ×1
chromium ×1
containers ×1
gulp ×1
jsx ×1
module ×1
reactjs ×1