JS Object-oriented, prototype, call (), apply ()

Source: Internet
Author: User

First, the cause of the day with Prototype.js so open to see, just see a few lines on the head fog, the reason is the object of JS is not very familiar with, so Baidu +google a handful, finally counted small have harvest, write this memorial ^_^.  Prototype.js Code snippet Copy code code is as follows: var Class = {Create:function () {return function () {this.initialize.apply (this, arguments); }}}//Class is used as follows var A = Class.create (); A. prototype={initialize:function (v) {this. value=v;} showvalue:function () {alert (this.value);}} var a = new A (' Hellow Ord! '); A. Showvalue ();//Popup dialog box helloword! What is L initialize? l What does the Apply method do? What about the L arguments variable? L Why is the Initialize method executed after new a? Find the answer: Second, JS object-oriented initialize is what? is simply a variable that represents a method, and the purpose is the constructor of the class. Its specific functions rely on JS object-oriented support, then JS object-oriented What is it like? What is the same and different from Java? Look at the code: Copy the Code as follows: var ClassName = function (v) {this.value=v; This.getvalue=function () {return this.value;} this.setvalue=fu Nction (v) {this.value=v;}} So what's the difference between a function and a class in JS? In fact, ClassName is a function that constructs an object as a constructor when it appears behind new. Copy the code as follows: var objectName1 = new ClassName ("a");//Get an object where objectName1 is the object that is obtained after the ClassName constructor is executed, The This in the ClassName function refers to the object that is constructed after new, so the objectName1 will be followed by a property and two methods. You can call them by doing this: copy generationCode codes are as follows: Objectname1.setvalue ("' Hello '); Alert (Objectname1.getvalue ());//Dialog Hello alert (objectname1.value);//Dialog Hello then copy code code as follows: var objectName2 = ClassName ("B");//Get an object So what does the objectName2 get? It is obviously the return value of the method, where classname is used only as a normal function (although the first letter is capitalized). But there is no return value in the previously written classname, so objectName2 will be undifinded so who is the "B" assigned to it? In this it does not produce an object, but simply executes this method, so this "B" assignment to the object called this method window, the evidence is as follows: var objectName2 = ClassName ("b");//Get an Object alert ( Window.value);//dialog B so all functions in JS are the same, but the use may be different (used as a construction object or a process). It's time to go back to the subject. What is initialize? Copy the code as follows: var Class = {Create:function () {return function () {this.initialize.apply (this, arguments)}}} var A = Cl Ass.create (); This code is to construct a function to copy to a, this function is the copy code code as follows: function () {this.initialize.apply (this, arguments);} and the following method is used to do the constructor function. When using this constructor to construct an object, the initialize variable of the constructed object is used to execute the Apply () method, and the use of apply () is said to continue to say initialize. This will contact the initialize when initializing the object (how to contact is to see apply). Then copy the code code as follows: a.prototype={initialize:function (v) {this. value=v;} showvalue:function () {alert (this.value);}} what does it mean?? Prototype is the meaning of "archetype". A is a function (), then A. Prototype, which is a variable in function, is actually an object. What method does this object have, then the object that the function produces has the method, therefore var a = new A (' helloword! '); A. Showvalue ();//Popup dialog box helloword! So a object will also have a initialize method, not only that, every object with a construct will have a initialize method, and in the previous said, the construction will call the constructor, the constructor will let initialize to invoke the Apply method, So in new A (' helloword! ') Initialize go back and call the Apply method. This is called an initialization method. Third, call () and apply () began to study the Apply (), on the Internet to find a few information, and combined with their own research, to understand the function of call () and apply (). Functions are basically the same as function (). Call (object,{},{} ...) or function (). Apply (object,[...]) The function of object called here is Funciton (), the difference is that the call parameter from the second start is passed to the Funciton, can be listed in turn "," separated. Instead, apply has only two parameters, and the second is an array that stores all the arguments passed to the function. This.initialize.apply (this, arguments); What is the meaning? The first this here refers to the object that is generated after the constructor is called with new, that is, the previous a, and the second one should also refer to the same object. Then this is this (that is, a) call the Initialize method, the parameter is the arguments object (the array object of the parameter), so when the constructor executes, object a executes the Initialize method to initialize, and the word "initialize "The meaning is on the right. So how do the arguments to execute the Initialize method pass in? Four, arguments object This code can explain everything: Copy the code as follows: function test () {alert (typeof arguments); for (var i=0; i<arguments. length; i++) {alert (arguments[i]);} } Test ("1", "2", "3"); Test ("A", "B"); After execution alert (typeof arguments), an object is displayed, indicating that arguments is an object. It then hits 1, 2, 3 in turn. Description arguments is the real parameter group that invokes the function. Copy the code code as follows: Var Class= { create:function () {return function () {this.initialize.apply (this, arguments);}}} Arguments is the real parameter group of the constructor returned by create, then at Var a= newA (' helloword! '); when ' helloword! ' Is the real parameter group (though there is only one string), passed to the method apply, and then passed as a parameter to the initialization function initialize when calling initialize. 

JS Object-oriented, prototype, call (), apply ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.