1、function 函数的bind方法:
Function.prototype._bind = function (context) {
var fn = this;
return function () {
console.log('init constructor');
fn.apply(context,arguments);
}
}
var foo = {x:"foo "};
var baz = function (str) {
console.log(this.x + (arguments.length === 0 ? '':str));
}
baz();
var _bind = baz._bind(foo);
_bind('baz');
call 方法 重写:
Function.prototype._call = function (context,args) {
var fn = this;
console.log('init constructor');
var self = context;
fn.call(self,args);
}
var foo = {x:"foo "};
var baz = function (str) {
console.log(this.x + (arguments.length === 0 ? '':str));
}
baz();
var _call = baz._call(foo,'barfd');
// _call('barza');
baz.call(foo,'barza');
2、正则表达式替换URL参数名:
3、javascript 静态方法的继承
function A() {
console.log('test a');
this.say = function () {
console.log('aaaa');
}
}
A.sayHello = function(){
console.log('test a static method');
};
function B() {
A.call(this);
console.log('test b');
this.sayB = function () {
console.log('this is b say');
}
}
B.sayHello = function () {
A.sayHello();
}
B.sayHello();
var b = new B();
b.say();
4、移动适配viewport的指定
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
5、js 深copy
var obj = {a: 1, b: 2, c: {d: 3, f: 5}};
function deepClone(obj, source) {
var result = source || {};
for (var key in obj) {
if (typeof obj[key] === 'object') {
result[key] = obj[key].constructor === Array ? [] : {};
deepClone(obj[key], result[key]);
} else {
result[key] = obj[key];
}
}
return result;
}
var array = deepClone(obj);
console.log(obj);
console.log(array);