日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

object.prototype.call

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 object.prototype.call 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

object.prototype.call

/* * object.prototype.call * @ 當(dāng)一個(gè)object沒(méi)有某個(gè)方法,但是其他的有,我們可以借助call或apply用其它對(duì)象的方法來(lái)操作。 * @ 語(yǔ)法: fun.call(thisArg[, arg1[, arg2[, ...]]]) * @ param: thisArg {object} //當(dāng)前引用對(duì)象 * @ 不傳參數(shù),傳null,undefined, this指向window對(duì)象 * @ 傳遞另一個(gè)函數(shù)的函數(shù)名fun2, this指向函數(shù)fun2的引用 * @ 傳遞一個(gè)對(duì)象,函數(shù)中的this指向這個(gè)對(duì)象 * @ 值為原始值(數(shù)字,字符串,布爾值), this會(huì)指向該原始值的自動(dòng)包裝對(duì)象,如String,Number,Boolean * @ param: arg1, arg2, ... {object} // arguments參數(shù) */

?

call函數(shù)中的this指向

function a(){console.log(this); } function b(){}var objthis = {name: "Alan"}; //定義對(duì)象 a.call(); // window a.call(null); // window a.call(undefined); // window a.call(1); // Number {[[PrimitiveValue]]: 1} a.call(""); // String {length: 0, [[PrimitiveValue]]: ""} a.call(true); // Boolean {[[PrimitiveValue]]: true} a.call(b); // function b(){} a.call(objthis); // Object {name: "Alan"}

?

使用call對(duì)象的構(gòu)造函數(shù)鏈

function Product(name, price){this.name = name;this.price = price;if(price < 0){throw RangeError('Cannot create product ' + this.name + ' with a negative price');} }// call方法 function Food(name,price){Product.call(this,name,price);this.category = "food"; }// 等同于 function Food(name,price){this.name = name;this.price = price;if(price < 0){throw RangeError('Cannot create product ' + this.name + ' with a negative price');}this.category = "food"; }

?

使用call調(diào)用匿名函數(shù)

var animals = [{species: "Lion",name: "king"},{species: "Whale",name: "Fail"} ]for(var i = 0; i < animals.length; i++){(function(i){this.print = function(){console.log("#" + i + " " + this.species + ": " + this.name);}this.print();}).call(animals[i],i);// 等同于/*(function(){this.print = function(){console.log("#" + i + " " + animals[i].species + ": " + animals[i].name);}this.print();})();*/ }

?

使用call調(diào)用函數(shù)的上下文this

function greet(){var reply = [this.person, "Is An Awesome", this.role].join(" ");console.log(reply); }var obj = {person: "Douglas Crockford", role: "Javascript Developer" };greet.call(obj);

?

以DOM為例子

function changeStyle(attr, value){this.style[attr] = value; } var box = document.getElementById('box'); window.changeStyle.call(box, "height", "200px"); window.changeStyle.apply(box, ['height', '200px']);

?

// 不用call?

function say(name){console.log(this + "," + name);}say.call2 = function( thisObj, arg1 ) {thisObj = new Object( thisObj );thisObj.say = this;return thisObj.say(arg1);};say.call2("hola","Mike");

?

轉(zhuǎn)載于:https://www.cnblogs.com/alantao/p/5882411.html

總結(jié)

以上是生活随笔為你收集整理的object.prototype.call的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。