var info = "This is global info";
var obj = {
info: 'This is local info',
getInfo: getInfo
}
function getInfo() {
console.log(this.info);
}
obj.getInfo() //This is local info
getInfo() //This is global info 当前上下文环境被修改了
var info = "This is global info";
var obj = {
info: 'This is local info',
getInfo: function getInfo() {
console.log(this.info);
var getInfo2 = function getInfo2() {
console.log(this.info);
}
getInfo2();
}
}
obj.getInfo();
//This is local info
//This is global info
var info = "This is global info";
var obj = {
info: 'This is local info',
getInfo: function getInfo() {
console.log(this.info);
var self = this; //将外层this保存到变量中
var getInfo2 = function getInfo2() {
console.log(self.info); //指向外层变量代表的this
}
getInfo2();
}
}
obj.getInfo();
//This is local info
//This is local info
... var anotherFun = obj.getInfo; anotherFun.call(obj) //This is local info
function add(num1, num2, num3) {
return num1 + num2 + num3;
}
add.call(null, 10, 20, 30); //60
add.apply(null, [10, 20, 30]); //60
Function.prototype.bind(context[, argument1, argument2])
//函数柯里化部分
function add(num1 ,num2) {
return num1 + num2;
}
var anotherFun = window.add.bind(window, 10);
anotherFun(20); //30
... var anotherFun = obj.getInfo.bind(obj) anotherFun(); //This is local info
if (!Function.prototype.bind) {
Function.prototype.bind = function (obj) {
var self = this;
return function () {
self.call(obj);
}
}
}
...
Function.prototype.bind = function(obj) {
var args = Array.prototype.slice.call(arguments, 1);
//记录下所有第一次传入的参数
var self = this;
return function () {
self.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
}
}
function fun(){console.log("function called")}
//直接调用
fun() //function called
//构造调用
var obj = new fun() //function called
var a = 5;
function Fun() {
this.a = 10;
}
var obj = new Fun();
obj.a //10
var obj1 = {
info: "this is obj1",
getInfo: () => console.log(this.info)
}
var obj2 = {
info: "this is obj2",
getInfo: () => console.log(this.info)
}
obj1.getInfo() //this is obj1 obj2.getInfo() //this is obj2 obj1.getInfo.call(obj2) //this is obj2 obj2.getInfo.call(obj1) //this is obj1
var obj = {}
function foo(num){
this.num = num;
}
var setNum = foo.bind(obj);
setNum(10);
obj.num //10
var obj2 = new setNum(20);
obj.num //10
obj2.num //20
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind -
what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP ?
this : oThis || this,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
var obj = {
num: 1,
getNum: function () {
return function () {
//this丢失
console.log(this.num);
//此处的this指向window
}
}
}
obj.getNum()(); //undefined
var obj2 = {
num: 2,
getNum: function () {
return () => console.log(this.num);
//箭头函数内部绑定外部getNum的this,外部this指向调用的对象
}
}
obj2.getNum()(); //2
if (!Function.prototype.softBind) {
Function.prototype.softbind = function (obj) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return self.apply((!this || this === (window || global)) ?
obj : this,
args.concat(Array.prototype.slice.call(arguments)));
}
}
}
var slice = Array.prototype.slice; slice(arguments); //error
var qsa = document.querySelectorAll; qsa(something); //error
var qsa = document.querySelectorAll.bind(document); qsa(something);
var slice = Function.prototype.call.bind(Array.prototype.slice); slice(arguments);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有