无标题文章

2017-12-05  本文已影响0人  BlackWingXu
function setupSomeGlobals() {
    // Local variable that ends up within closure
    var num = 666;
    // Store some references to functions as global variables
    gAlertNumber = function() { alert(num); }
    gIncreaseNumber = function() { num++; }
    gSetNumber = function(x) { num = x; }
}
/*setupSomeGlobals(); // 为三个全局变量赋值
gAlertNumber(); //666
gIncreaseNumber();
gAlertNumber(); // 667
gSetNumber(12);//
gAlertNumber();//12*/

function myClosure(X){
    var temp="Hello "+X;
    return  function () {
        console.log(temp)
    }
}
myClosure("closure")()
var iNum=10;
function add(num1,num2){
        function doAdd(){return num1+num2+iNum;}
        return doAdd();
}
var name = "The Window";
var object = {
    name : "My Object",
    getNameFunc : function(){
        return function(){
            return this.name;
        };
    }
};
console.log(object.getNameFunc()());
function  va() {
   var  name = "My Object";

    return    function getNameFunc() {
        return name;
    }
}
console.log(va()())
var name = "The Window";
var object = {
    name : "My Object",
    getNameFunc : function(){
        var that = this;
        return function(){
            return that.name;
        };
    }
};
console.log(object.getNameFunc()());
上一篇下一篇

猜你喜欢

热点阅读