<!doctype html>
<html>
<head>
<title>标题</title>
<meta charset="utf-8">
<script>
if(Function.prototype.bind===undefined){
Function.prototype.bind=
function(obj/*,args1*/){
var fun=this;//原函数对象
//arguments:[obj,xx,xx,xx,]
var args1=
Array.prototype.slice.call(
arguments,1
);//arguments.slice(1)
return function(/*args2*/){
//arguments=>args2
//类数组对象转数组
var args2=
Array.prototype.slice.call(
arguments/*,?*/
);//arguments.slice()
//this->obj替换this
fun.apply(
obj,args1.concat(args2));
}
}
}
function calc(base,bonus){
document.write(this.name+
"的总工资是"+(base+bonus)+"<br>");
}
//this:和定义的位置无关!
// 只和调用时,.前的对象有关
var lilei={name:"Li Lei"};
var hmm={name:"hmm"};
calc.call(lilei,10000,5000);//临时借用
calc.call(hmm,9000,8000);//临时借用
var lilei_calc=calc.bind(lilei,10000);
//lilei_calc:function(){//永久绑定
//this->lilei ; base->10000
//calc
//}
lilei_calc(5000);
lilei_calc(6000);
var hmm_calc=calc.bind(hmm,9000)
hmm_calc(8000);
</script>
</head>
<body>
</body>
</html>
javascript面向对象(十五)
最新推荐文章于 2024-01-18 09:27:42 发布