需要输出帮助

时间:2017-11-21 06:58:28

标签: javascript typescript

任何人都可以回答这个问题吗?

以下代码输出到控制台的原因是什么?

(function(){    
  var a = b = 3;    
})();
console.log("a defined? " + (typeof a !== 'undefined'));    
console.log("b defined? " + (typeof b !== 'undefined'));

以下代码输出到控制台的原因是什么?

var myObject = { 
    foo: "bar",    
    func: function() {    
        var self = this;    
        console.log("outer func:  this.foo = " + this.foo);    
        console.log("outer func:  self.foo = " + self.foo);    
        (function() {    
            console.log("inner func:  this.foo = " + this.foo);   
            console.log("inner func:  self.foo = " + self.foo); 
        }());    
    }    
};    
myObject.func();

3 个答案:

答案 0 :(得分:4)

代码的

第一部分等同于此。



(function(){
  var a;
  b = 3;
  a = b;
})();

console.log("a defined? " + (typeof a !== 'undefined'));

console.log("b defined? " + (typeof b !== 'undefined'));




在这种情况下,a在当前函数中声明,与当前范围相关,并且在其范围之外不可见。所以它在外部范围内是undefined。但是b被声明为全局变量,因此它随处可见而不是undefined,其值为3

最后一个代码部分怎么样,每个函数都有自己的上下文,this引用了它。对于第一级函数(在某些编程语言中也称为方法),this指的是调用它的当前对象。但是对于func内部声明的内部函数,它有自己的上下文,this指的是上下文。那么为什么this引用undefined - 而不是对象。但是使用另一个变量如self在该函数的外部范围内声明,您可以访问外部变量。关键部分与名为this的变量相关。这很特别。



var myObject = { 
    foo: "bar",    
    func: function() {    
        var self = this;    
        console.log("outer func:  this.foo = " + this.foo);    
        console.log("outer func:  self.foo = " + self.foo);    
        (function() {    
            console.log("inner func:  this.foo = " + this.foo);   
            console.log("inner func:  self.foo = " + self.foo); 
        }());    
    }    
};    
myObject.func();




答案 1 :(得分:1)

第一个代码:

(function(){    
  var a = b = 3;    
})();
console.log("a defined? " + (typeof a !== 'undefined'));    
console.log("b defined? " + (typeof b !== 'undefined'));

<强>输出:

a defined? false
b defined? true

<强>阐释: 在这里,您使用立即调用函数表达式(IIFE)。这里a在函数范围内声明,但b未被声明,因为它被注册到全局范围。现在,由于您在全球范围a之外访问baundefinedbdefinedvar myObject = { foo: "bar", func: function() { var self = this; console.log("outer func: this.foo = " + this.foo); console.log("outer func: self.foo = " + self.foo); (function() { console.log("inner func: this.foo = " + this.foo); console.log("inner func: self.foo = " + self.foo); }()); } }; myObject.func();

第二代码:

outer func:  this.foo = bar
outer func:  self.foo = bar
inner func:  this.foo = undefined
inner func:  self.foo = bar

<强>输出:

this

<强>阐释:

这里的关键是函数内的logs引用,该函数是对象本身的属性。因此,前两个this打印预期结果。然后你正在使用IIFE,IIFE中的global Object(Window object)引用是this.foo。因此,Window.foo表示undefinedself。但是foo可以在该函数内访问,该函数的值等于该对象。因此它打印chrome://flags/#disable-accelerated-2d-canvas变量。

答案 2 :(得分:1)

以下代码是一个n立即调用函数 变量的范围a&amp; b位于函数内部,但console.log位于function之外,因此它们将未定义

console.log("a defined? " + (typeof a !== 'undefined')); //output a defined false
console.log("b defined? " + (typeof b !== 'undefined')); //output b defined? true

&#13;
&#13;
(function() {
  var a = b = 3;
})();
console.log("a defined? " + (typeof a !== 'undefined'));
console.log("b defined? " + (typeof b !== 'undefined'));
&#13;
&#13;
&#13;

在下面的代码段中添加了评论,希望它会有用

&#13;
&#13;
var myObject = {
  foo: "bar",
  func: function() {
    // here this & self will refer to the myObject
    var self = this;
    console.log("outer func:  this.foo = " + this.foo); //outer func:  this.foo = bar    
    console.log("outer func:  self.foo = " + self.foo); //outer func:  self.foo = bar    
    (function() {
      // here self will refer to the myObject context where as 
      // this will refer to window object.
      // In window foo is not defined, so it will give undefined
      console.log("inner func:  this.foo = " + this.foo); //inner func:  this.foo = undefined  
      console.log("inner func:  self.foo = " + self.foo); //inner func:  self.foo = bar
    }());
  }
};
myObject.func();
&#13;
&#13;
&#13;