码迷,mamicode.com
首页 > 其他好文 > 详细

MDN上的示例代码

时间:2017-08-23 19:14:53      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:undefined   nts   out   bin   cti   define   使用   time   span   

1.bind方法时,提到了偏函数的用法,给函数提供一个默认的参数。


var
list = function(){ return Array.prototype.slice.call(arguments); } var list1 = list(1,2,3); console.log(list1); //创建一个函数,有一个默认的起始参数 var plist = list.bind(undefined,5); console.log(plist()); console.log(plist(1,2,3));
2. setTimeOut()时,用bind绑定this的值。

function flower(){
    this.petalCount = Math.ceil(Math.random()*12)+1;
}
flower.prototype.declare = function(){
    console.log("I am an beautiful flower with "+this.petalCount+" petals.");
}
flower.prototype.bloom = function(){
    global.setTimeout(this.declare.bind(this),1000);
}

var aFlower = new flower();
aFlower.bloom();
3.setTimeOut 的使用
var
array = [‘one‘,‘two‘,‘three‘]; Array.prototype.myMethod = function(passed){ return arguments.length>0?this[passed]:this; } console.log(array.myMethod()); // one two three console.log(array.myMethod(1)) //two // setTimeout(function() { // console.log(array.myMethod()) // }, 1000);

 

MDN上的示例代码

标签:undefined   nts   out   bin   cti   define   使用   time   span   

原文地址:http://www.cnblogs.com/goodearth/p/7419596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!