小编Mic*_*iot的帖子

通过window对象引用它们时,全局变量未定义

我通过窗口全局声明变量是相当新的,所以我有点惊讶,以下代码段的行为取决于浏览器.

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";该范围,但我对浏览器为何以不同方式处理它感兴趣.

http://jsfiddle.net/WHYFc/

javascript window reference undefined

6
推荐指数
1
解决办法
4768
查看次数

如何创建一个不使用ES6类从Object.prototype继承的类?

我可以创建一个不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); //inheritance
Run Code Online (Sandbox Code Playgroud)

我怎样才能使用ES6课程?

class Shape {
  constructor(x, y, width, height) {
    this.x …
Run Code Online (Sandbox Code Playgroud)

javascript null prototype class ecmascript-6

6
推荐指数
1
解决办法
169
查看次数