Skip to content

Instantly share code, notes, and snippets.

@lyuehh
Created October 22, 2012 03:07
Show Gist options
  • Save lyuehh/3929431 to your computer and use it in GitHub Desktop.
Save lyuehh/3929431 to your computer and use it in GitHub Desktop.
js func
//方法调用模式
var obj = {
val: 0,
inc: function(i) {
this.val += i;
}
};
console.log(obj.val);
obj.inc(2);
obj.inc(2);
console.log(obj.val);
// 函数调用模式
var add = function(a,b) {
return a + b;
};
console.log(add(1,4));
obj.double = function () {
var that = this;
var helper = function() {
that.val = add(that.val, that.val);
};
helper();
};
obj.val = 5;
obj.double();
console.log(obj.val);
// 构造器调用
var Q = function(str) {
this.status = str;
};
Q.prototype.getStatus = function() {
return this.status;
};
var myQ = new Q('haha');
​console.log(myQ.getStatus());
// //avoid this..
// var yourQ = Q('haha');
// console.log(yourQ.getStatus());
// apply调用模式
var array = [3,4];
console.log(add.apply(null,array));
console.log(add.call(null,3,4));
var myStatus = {
status: 'ok..'
};
console.log(Q.prototype.getStatus.apply(myStatus));
@lyuehh
Copy link
Author

lyuehh commented Oct 23, 2012

Function.prototype.method = function(name, func) {
    this.prototype[name] = func;
    return this;
};

var m = function(spec) {
    var that = {};

    that.get_name = function() {
        return spec.name;
    };
    that.says = function() {
        return spec.saying || '';
    };

    return that;
};
var m1 = m({name: 'Herb'});
console.log(m1.get_name());

var cat = function(spec) {
    spec.saying = spec.saying || 'meow';
    var that = m(spec);
    that.purr = function(n) {
        return n + 'ss';
    };
    that.get_name = function() {
        return 'meow name: ' + spec.name;
    };
    return that;
};
var myCat = cat({name: 'Haha'});
console.log(myCat.get_name());

Object.method('super', function(name) {
    var that = this,
        method = that[name];
    return function() {
        return method.apply(that, arguments);
    };
});

var coolcat = function(spec) {
    var that = cat(spec),
        super_get_name = that.super('get_name');
    that.get_name = function(n) {
        return 'like ' + super_get_name() + ' baby';
    };
    return that;
};

var myCoolCat = coolcat({name: 'cool'});
console.log(myCoolCat.get_name());

@lyuehh
Copy link
Author

lyuehh commented Oct 23, 2012

var eventuality = function(that) {
    var registry = {};

    that.fire = function(event) {
        var array,
            func,
            handerm,
            i,
            type = typeof event === 'string' ?
                event : event.type;
        if(registry.hasOwnProperty(type)) {
            array = registry[type];
            for (i=0; i < array.length; i++) {
                handler = array[i];
                func = handler.method;
                if(typeof func === 'string') {
                    func = this[func];
                }
                func.apply(this, handler.parameters || [event]);
            }
        }
        return this;
    };
    that.on = function(type, method, parameters) {
        var handler = {
            method: method,
            parameters: parameters
        };
        if(registry.hasOwnProperty(type)) {
            registry[type].push(handler);
        } else {
            registry[type] = [handler];
        }
        return this;
    };
    return that;
};

var a = {name: 'a'};
eventuality(a);
a.on('xx', function(s) {
    // 这里的s 是fire的参数
    console.log(this.name + s);
});
// 貌似fire不能传递自定义参数
a.fire('xx');

var b = {name: 'b',age: 21};
eventuality(b);
// on 可以设置自定义参数,fire时会自动调用,但是不能在fire时修改
b.on('xx',function(s) {
    // 这里的s是默认的参数
    console.log('xx' + s.c + s.d);
},[{c: 'c', d:'d'}]);
b.fire({type: 'xx'});

@lyuehh
Copy link
Author

lyuehh commented Oct 23, 2012

var eventuality = function(that) {
    var registry = {};
    that.fire = function(event) {
        var array, func, handerm, i, type = typeof event === 'string' ? event : event.type;
        if (registry.hasOwnProperty(type)) {
            array = registry[type];
            for (i = 0; i < array.length; i++) {
                handler = array[i];
                func = handler.method;
                if (typeof func === 'string') {
                    func = this[func];
                }
                // 如果传递了自定义参数,则使用自定义参数,否则使用默认的 
                var parameters = (typeof event === 'object' && event.parameters) ? event.parameters : null;
                //console.log('param: ' + parameters);
                func.apply(this, parameters || handler.parameters);
            }
        }
        return this;
    };
    that.on = function(type, method, parameters) {
        var handler = {
            method: method,
            parameters: parameters
        };
        if (registry.hasOwnProperty(type)) {
            registry[type].push(handler);
        } else {
            registry[type] = [handler];
        }
        return this;
    };
    return that;
};
var a = {
    name: 'a'
};
eventuality(a);
a.on('xx', function(s) { 
    console.log('name: ' + this.name + ' p: ' + s.p);
},[{p: 'default'}]);
a.fire({type: 'xx', parameters: [{p: 'new'}]});
a.fire('xx');

@lyuehh
Copy link
Author

lyuehh commented Oct 23, 2012

Function.prototype.method = function(name, func) {
    this.prototype[name] = func;
    return this;
};

Array.method('reduce', function(f, value) {
    for (var i = 0; i < this.length; i++) {
        value = f(this[i], value);
    };
    return value;
});

var add = function(a, b) {
    return a + b;
};
var mult = function(a, b) {
    return a * b;
};

var data = [1, 2, 3, 4, 5];
var sum = data.reduce(add, 0);
var mult = data.reduce(mult, 1);
console.log('sum: ' + sum);
console.log('mult: ' + mult);

data.total = function() {
    return this.reduce(add, 0);
};
console.log('total: ' + data.total());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment