最新javascript prototype詳解優質
在日常的學習、工作、生活中,肯定對各類范文都很熟悉吧。那么我們該如何寫一篇較為完美的范文呢?下面我給大家整理了一些優秀范文,希望能夠幫助到大家,我們一起來看一看吧。
javascript prototype詳解篇一
以前,你可能會直接設置self=this或者that=this等等,這樣做當然也能起作用,()會更好,看上去也更專業。
下面舉個簡單的例子:
復制代碼 代碼如下:
var myobj = {
specialfunction: function () {
},
anotherspecialfunction: function () {
},
getasyncdata: function (cb) {
cb();
},
render: function () {
var that = this;
ncdata(function () {
lfunction();
rspecialfunction();
});
}
};
();
在這個例子中,為了保持myobj上下文,設置了一個變量that=this,這樣是可行的,()看著更整潔:
復制代碼 代碼如下:
render: function () {
ncdata(function () {
lfunction();
rspecialfunction();
}.bind(this));
}
()時,它會簡單的創建一個新的函數,然后把this傳給這個函數。()的代碼大概是這樣的:
復制代碼 代碼如下: = function (scope) {
var fn = this;
return function () {
return (scope);
};
}
()的.例子:
復制代碼 代碼如下:
var foo = {
x: 3
};
var bar = function(){
(this.x);
};
bar(); // undefined
var boundfunc = (foo);
boundfunc(); // 3
是不是很好用呢!不過遺憾的是ie8及以下的ie瀏覽器并不支持()。支持的瀏覽器有chrome 7+,firefox 4.0+,ie 9+,opera 11.60+,safari 5.1.4+。雖然ie 8/7/6等瀏覽器不支持,但是mozilla開發組為老版本的ie瀏覽器寫了一個功能類似的函數,代碼如下:
復制代碼 代碼如下:
if (!) {
= function (othis) {
if (typeof this !== "function") {
// closest thing possible to the ecmascript 5 internal iscallable function
throw new typeerror(" - what is trying to be bound is not callable");
}
var aargs = (arguments, 1),
ftobind = this,
fnop = function () {},
fbound = function () {
return (this instanceof fnop && othis
? this
: othis,
((arguments)));
};
ype = ype;
ype = new fnop();
return fbound;
};
}
s("content_relate");【()方法介紹】相關文章:
1.
javascript中的dom方法
2.javascript tofixed方法介紹
3.獲取javascript中的方法
4.關于javascript中的包裝對象介紹
5.javascript數組常用方法介紹
6.詳解javascript中的splice()使用方法
7.關于異步javascript編程中的promise使用方法
8.javascript應用到網頁中的方法