Several methods js function call

js function call parameters will be given away for free, and this is two out of arguments. arguments are the arguments group, he is not a true array, but the length can be obtained using the method .length.

The book says there are 4 ways to call:

  • Method call mode
  • Function call mode
  • Constructor calls mode
  • apply call mode

Let us look at some examples of better understanding.

1: method call mode.

Please note that at this time this point myobject.

/ * Method call mode * /
    var myObject = {
            value: 0,
            inc is: function () {
                    Alert (this.value)
                }
        }
    myobject.inc ()

 

2: function call mode

Please note that this window at this time point

/ * Function call mode * /
    
    var = the Add function (A, B) {
        Alert (the this) // top tied to the this window
            return A + B;
        }
    var = SUM the Add (3,4-);
    Alert (SUM)

 

3: The constructor calls mode

javascript essence of a language in this book is recommended to abandon the way. Because there is a better way. First here is not introduced. Bowen next time posted.

I will add a link here.

/ * Constructor calls abandoned mode * /
    
    var Quo = function (String) {
            this.status = String;
        }
    quo.prototype.get_status = function () {
            return this.status;
        }
    var = QQ new new Quo ( "AAA") ;
    Alert (qq.get_status ());

 

4: apply invocation pattern

== We can look at a more useful example apply. Look at the bottom of the code.

 

/ * Apply * /
    // Note the use of the above sum function
    // with myObject
    // This is called the advantage that it can point to this as an object.
    The first parameter is // apply this pointer to point to the object
    var ARR = [10,20];
    var add.apply = SUM (myObject, ARR);
    Alert (SUM);

 

Look at this apply real applications. This is a function of time bind binding

the bind function = var (Object, type, Fn) {
            IF (object.attachEvent) {// IEs browser
                    object.attachEvent ( "ON" + type, (function () {
                                                           return function (Event) {
                                                               window.event.cancelBubble = true; // stop time bubbling
                                                               object.attachEvent = [fn.apply (object)] ; // ---- here I want to say here
                                                               // add a attachEvent after a time bind in IE.
                                                               / / this point not to object so the object itself. function we bound in the this.id is not working properly.
                                                               // but if we use fn.apply (object)
                                                               // Here we can see that the first object is to apply the point is, this change to the object so this.id became
                                                               //object.id can work properly.
                                                               
                                                               }
                                                           }) (Object), to false);
                } the else IF (object.addEventListener) {// other browsers
                        object.addEventListener (type, function (Event) {
                                                              that event.stopPropagation (); // stop time effervescence
                                                              fn.apply (the this)
                                                              });
                    }
            
        }
    bind(document.getElementById("aaa"),"click",function(){alert(this.id)}); 

Guess you like

Origin www.cnblogs.com/Annely/p/11925647.html