我通过窗口全局声明变量是相当新的,所以我有点惊讶,以下代码段的行为取决于浏览器.
window.test = "Good";
document.write(window.test);
document.write('<br>');
document.write(window.test);
document.write('<br>');
document.write(test);?
Run Code Online (Sandbox Code Playgroud)
Firefox,IE,Opera
好
未定义
好
Chrome和Safari
好
好
好
我最初的信念是它应该像Chrome和Safari那样表现,但我意识到我可能没有正确理解窗口对象,那么更有知识的人会解释这个吗?
我意识到我可以只使用var test = "Good";该范围,但我对浏览器为何以不同方式处理它感兴趣.
我可以创建一个不Object.prototype使用旧语法继承的类.
function Shape(x, y, width, height) {
this.x = x,
this.y = y,
this.width = width,
this.height = height;
}
Shape.prototype = Object.create(null, {
constructor: {
configurable: true,
writable: true,
value: Shape
},
move: {
configurable: true,
writable: true,
value: function (x, y) {
this.x += x,
this.y += y;
}
}
});
var rect = new Shape(0, 0, 4, 2);
console.log(Object.getPrototypeOf(rect) === Shape.prototype);
console.log(Object.getPrototypeOf(Object.getPrototypeOf(rect)) !== Object.prototype); //inheritanceRun Code Online (Sandbox Code Playgroud)
我怎样才能使用ES6课程?
class Shape {
constructor(x, y, width, height) {
this.x …Run Code Online (Sandbox Code Playgroud)javascript ×2
class ×1
ecmascript-6 ×1
null ×1
prototype ×1
reference ×1
undefined ×1
window ×1