if (!('bind' in Function.prototype)) {
Function.prototype.bind = function () {
var fn = this,
context = arguments[0],
args = Array.prototype.slice.call(arguments, 1);
return function () {
return fn.apply(context, args.concat(arguments));
}
}
}
优化JavaScript的构造函数(new关键字的使用)
方法一:
function User(name, age) {
if (typeof Object.create === 'undefined') {
Object.create = function (prototype) {
function C() {
C.prototype = prototype;
return new C();
}
}
}
var self = this instanceof User ? this : Object.create(User.prototype);
self.name = name;
self.age = age;
return self;
}
方法二: