2017年3月3日 星期五

(js)閉包問題

function Test() {
    var id = 0;

    __construct();

    function __construct() {
        id++;
    }

    this.getID = function() {
        console.log(id++);
    };
   
    if(!(this instanceof Test)){
    return function(){
    console.log(id++);
    }
    }
}

/* ========================================== */
var x = new Test();
x.getID();
x.getID();

var y = new Test();
y.getID();
y.getID();

console.log('-----------------------------');

var a = Test();

a();
a();
console.log('-----------------------------');

var b = Test();

b();
b();