javascript - this pointing problem in js extended prototype method
漂亮男人
漂亮男人 2017-05-19 10:36:17
0
1
591

The book JavaScript Ninja Secrets has a prototype library and an example of function bind code:

Function.prototype.bind = function(){
            var fn = this, args = Array.prototype.slice.call(arguments),
                object = args.shift();
            return function(){
                  return fn.apply(object,args.concat(Array.prototype.slice.call(arguments)));
            };
        }; 
            var myObject = {a:"1"};
            function myFunction(){
                return this == myObject;
            };
            var aFunction = myFunction.bind(myObject);

            aFunction();

I use breakpoints to check the functionbindThe fn inside points to myFunctionI don’t quite understand this function. My understanding is that just use Function.prototype Prototype extension method The variable var fn=this; in the declared variable fn=this; points to the function using this method, just like myFunction in this example. bind(myObject);Call the bin method, fn points to myFunction I don’t know if this function is correct or not

漂亮男人
漂亮男人

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!