javascript之面向对象,去看看《JavaScript权威指南》

解决方案 »

  1.   

    第一个bind是自定义对象
    第二个bind是给函数类添加一个方法apply,百度的:
    JavaScript中Apply方法目的是:应用某一对象的一个方法,用另一个对象替换当前对象。
    使用方法为apply([thisObj[,argArray]])其中:
    thisObj是可选项。将被用作当前对象的对象。
    argArray是可选项。将被传递给该函数的参数数组。另外:
    如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj, 并且无法被传递任何参数。
      

  2.   

    Function那里的bind实现了什么功能?bind对象又实现了什么功能呢?new以后还能执行到输出4321432么?
      

  3.   

    apply的说明如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。apply应用另一个函数,函数(类)就具备了另一个函数(类)的方法或者是属性,也就是我们说的继承
      

  4.   

    我举一个简单的例子给你看吧:
    <script>
    function adApplyDemo(x) { 
        return ("this is never-online, BlueDestiny '" + x + "' demo"); 

    function handleAdApplyDemo(obj, fname, before) { 
      var oldFunc = obj[fname]; 
      obj[fname] = function() { 
        return oldFunc.apply(this, before(arguments)); 
      }; 

    function hellowordFunc(args) { 
      args[0] = "hello " + args[0]; 
      return args; 

    function applyBefore() { 
        alert(adApplyDemo("chinmo")); 

    function applyAfter() { 
        handleAdApplyDemo(this, "adApplyDemo", hellowordFunc); 
        alert(adApplyDemo("chinmo")); // Hello chinmo! 

    </script>
    <input type="button" onclick="applyBefore()" value="原始的adApplyDemo('chinmo')"/> 
        <input type="button" onclick='applyAfter()' value="应用后的adApplyDemo('chinmo')"/> 
    这里需注意的是,你要先点"原始的adApplyDemo('chinmo')"按钮,因为先点"应用后的adApplyDemo('chinmo')"按扭,会先应用了apply方法,这样原始的值将会被改变。
      

  5.   

    apply(this,arguments)
    call(this,arguments[0],arguments[1]...........
      

  6.   

    function bind(obj, method) {
        var args = [];
        for (var ii = 2; ii < arguments.length; ii++) {
            args.push(arguments[ii]);加入数组
    BlueDestiny 的例子哦
    很不错 他是我偶像
      

  7.   

    bhtfg538 : 
     BlueDestiny 是什么人?你在哪看到这个的?
      

  8.   

    BlueDestiny都不知道?
    我师傅的师傅 超级强大的人 
    cnblogs 很多他的经典文章
      

  9.   

    理解这段代码要从以下几个方面入手
    第一,了解apply的功能
    apply是函数对象的方法,大部分js文档中都有解释,在这段代码中的用法有_obj[method].apply(_obj, _args)
    如果_args是[1,2,3]的话,等价于_obj.method(1,2,3)
    method.apply(_obj, _args)
    如果_args是[1,2,3]的话,同样等价于_obj.method(1,2,3)
    bind.apply(null, argv)
    如果_args是[1,2,3]的话,等价于bind(1,2,3)第二,了解function bind(obj, method)的功能
    bind把一个对象,一个函数和一组参数绑定在一起,返回一个新的函数
    比如,b = bind(obj, method,1,2,3);
    执行b()等价于obj.method(1,2,3)第三,了解Function.prototype.bind = function(context)的功能
    这段代码给系统中每一个函数增加了bind的功能
    比如,b2 = obj.method.bind(obj,1,2,3);
    执行b2()等价于,obj.method(1,2,3)第四,不要太关注obj()的定义和实例化(var c = new obj();)
    这段代码对bind和Function.prototype.bind功能的演示不是很清晰
    c = new Object();
    function getName(pre1, pre2)
    {
    return pre1+':'+pre2+':'+this.name;
    }
    c.name = 'C';
    c.getName = getName;
    b = c.getName.bind(c,'hello','world')
    b()
    这段代码会清楚一些b()的执行结果是"hello:world:C",这就是绑定的结果第五,bind的应用
    用来构建特别的callback
    在javascript中,一个对象的方法是不能直接用来做callback的,因为方法中对于this的引用是根据上下文的变化而变化的
    借用上面getName的例子,定义一个新函数callFunction
    function callFunction(cb) { return cb(); }
    执行,callFunction(c.getName);得到"undefined:undefined:window"不仅无法绑定pre1和pre2,就连c.name都没能正确返回
    执行callFunction(b);得到"hello:world:C",是我们想要的结果
      

  10.   

    啊,,,我弄懂了apply,但还没理解透,不知道为啥要搞得这么麻烦!
      

  11.   

    是为了弥补javascript不能把成员函数作为回调函数的限制
    也弥补了回调函数不能包含参数的限制
    用bind就可以把参数以及拥有这个函数的对象一起打包
    而apply是js提供的便利工具,让bind的功能可以很容易的实现,通过bind存储的对象和参数可以在需要的时候取出来,应用(apply)到我们需要的特定函数上