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));