Wednesday, September 28, 2016

js tricks

function fn() {
    this.a = 0;
    this.b = function() {
        alert(this.a)
    }
}
fn.prototype = {
    b: function() {
        this.a = 20;
        alert(this.a);
    },
    c: function() {
        this.a = 30;
        alert(this.a);
    }
}

var myfn = new fn();
myfn.b();
myfn.c();

// result
// 0
// 30


2.

function fun(n,o) {
    console.log(o)
    return {
        fun:function(m){
            return fun(m,n);
        }
    };
}

var a = fun(0); a.fun(1); a.fun(2); a.fun(3);
// undefined, 0 0 0
var b = fun(0).fun(1).fun(2).fun(3);
var c = fun(0).fun(1); c.fun(2); c.fun(3);
// undefined, 0, 1,2
// undefined, 0,1, 1,



reference: web.jobbole.com/88041/