alert(this) //window
function fooCoder(x) {
this.x = x;
}
fooCoder(2);
alert(x);
// 全局变量x值为2
var name = "clever coder";
var person = {
name : "foocoder",
hello : function(sth){
console.log(this.name + " says " + sth);
}
}
person.hello("hello world");
new FooCoder();
var name = "clever coder";
var person = {
name : "foocoder",
hello : function(sth){
var sayhello = function(sth) {
console.log(this.name + " says " + sth);
};
sayhello(sth);
}
}
person.hello("hello world");
//clever coder says hello world
var name = "clever coder";
var person = {
name : "foocoder",
hello : function(sth){
var that = this;
var sayhello = function(sth) {
console.log(that.name + " says " + sth);
};
sayhello(sth);
}
}
person.hello("hello world");
//foocoder says hello world
person.hello.call(person, "world");
call( thisArg [,arg1,arg2,… ] ); // 参数列表,arg1,arg2,... apply(thisArg [,argArray] ); // 参数数组,argArray
obj.foocoder(); foocoder.call(obj, ...); foocoder.apply(obj, …);
$("#some-ele").click = obj.handler;
fun.bind(thisArg[, arg1[, arg2[, ...]]]) thisArg
$("#some-ele").click(person.hello.bind(person));
//相应元素被点击时,输出foocoder says hello world
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有