JavaScript inheritance mechanism & call Apply usage instructions

Source: Internet
Author: User
<span id="Label3"></p><p><p>I. The mechanism of succession</p></p><p><p>1. Object Impersonation: The constructor uses the This keyword to assign values to all properties and methods, which makes the ClassA constructor a ClassB method and then calls it.</p></p><pre><pre><span style="color: #000000;">function Classz () { </span>this. newmethod =<span style="color: #000000;"> classx; </span> <span style="color: #0000ff;"></span> <span style="color: #0000ff;"></span> this <span style="color: #000000;">. Newmethod (); </span> <span style="color: #0000ff;"></span> this <span style="color: #000000;">. newmethod; </span> <span style="color: #0000ff;"></span>This. newmethod =<span style="color: #000000;"> classy; </span> <span style="color: #0000ff;"></span> this <span style="color: #000000;">. Newmethod (); </span> <span style="color: #0000ff;"></span> this <span style="color: #000000;">. newmethod;}</span></pre></pre><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->There is a drawback here, that if there are two classes CLASSX and classy have properties or methods with the same name, classy has a high priority. Because it inherits from the following class. In addition to this small problem, it is easy to use object impersonation to implement multiple inheritance Mechanisms.</p></p><p><p>2, Apply (), Call <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--> () method is the most similar to the Classic object impersonation method</p></p><pre><pre><span style="color: #000000;">function ClassB (scolor, sName) { </span><span style="color: #008000;">//</span><span style="color: #008000;">this.newmethod = ClassA; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">This.newmethod (color); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Delete this.newmethod;</span> Classa.call (<span style="color: #0000ff;"></span>This<span style="color: #000000;">, scolor); </span> <span style="color: #0000ff;"></span>this. name =<span style="color: #000000;"> sName; </span> <span style="color: #0000ff;"></span>This. sayname =<span style="color: #000000;"> function () { alert (</span>this<span style="color: #0000ff;"></span><span style="color: #000000;">. name);} ;}</span></pre></pre><p><p>3. Prototype chain</p></p><pre><pre><span style="color: #000000;"></span><span style="color: #800000;">"</span> <span style="color: #800000;">Blue</span> <span style="color: #800000;">"</span> <span style="color: #000000;"></span> =<span style="color: #000000;"> function () { alert (</span><span style="color: #0000ff;"></span><span style="color: #000000;"></span>This<span style="color: #0000ff;">new</span> ClassA ();</pre></pre><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--></p></p>Set the prototype property of the ClassB to an instance of ClassA<span style="color: #ff0000;"><span style="color: #ff0000;">Note</span></span>: called the constructor of ClassA and did not pass arguments to it. This is standard practice in the prototype Chain. Make sure that the constructor does not have any Parameters. second, Call & Applyapply: method can hijack another Object's method, inherit the property of another Object.  The Function.apply (obj,args) method can receive two parameters call: the same as apply, except that the parameter list is Different. Function.call (obj,[param1[,param2[,... [, paramn]]]) <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--><strong><strong>Object inheritance, The general practice is to replicate:</strong></strong>Object.extend<pre><pre>Object.extend =<span style="color: #000000;"> function (destination, source) { for in</span><span style="color: #0000ff;"></span><span style="color: #0000ff;"></span> <span style="color: #000000;"> source) { </span>= <span style="color: #000000;"> source[property]; } </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">destination;}</span></pre></pre><p><p>In addition, there is a way, is: function.apply or Function.call. <!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->The value of this can be set by the call () or the Apply () method and is called as a new method of an existing Object.</p></p>Under JavaScript Strict mode (strict mode), when a function is called, the first parameter becomes the value of this, even if the parameter is not an Object. In JavaScript's non-strict mode, If the value of the first parameter is null or undefined, it uses the global object Override. non-strict<p><p><strong>Usage scenarios:</strong></p></p><p><p>1. <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--> arguments convert to an array</p></p><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--></p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">The return is an array, but the arguments itself remains</span> constant vararg=<span style="color: #000000;">[].slice.call (arguments);</span> <span style="color: #008000;">//</span><span style="color: #008000;"></span></pre></pre><p><p>2. borrowing</p></p><pre><pre><span style="color: #0000ff;">var</span> foo =<span style="color: #000000;"> { </span><span style="color: #800000;">'</span><span style="color: #800000;">joker</span><span style="color: #800000;">'</span><span style="color: #000000;">, showname:function () { Console.log (</span><span style="color: #0000ff;"></span>this<span style="color: #000000;">. name); </span> <span style="color: #0000ff;">var</span> bar =<span style="color: #000000;"> { </span><span style="color: #800000;">'</span><span style="color: #800000;">rose</span><span style="color: #800000;">'</span> <span style="color: #000000;"> } foo.showName.call (bar); </span></pre></pre><p><p>3. Inheritance</p></p><pre><pre><span style="color: #0000ff;">var</span> Student =<span style="color: #000000;"> function (name, age, high) { </span><span style="color: #008000;">//</span> <span style="color: #008000;"> </span>use call person.call (<span style="color: #0000ff;"></span>this<span style="color: #000000;">, name,age); </span> <span style="color: #0000ff;"></span>this. high= high<span style="color: #000000;">;}</span></pre></pre><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--></p></p><p><p>Person.apply (this,arguments);</p></p><p><p>This: in the creation of the object at this time represents the student</p></p><p><p>Arguments: is an array, i.e. ["qian", "21", "first grade"];</p></p><p><p>Using student to execute the contents of the person class, there are statements such as this.name in the person class, so that the attribute is created into the student Object.</p></p>In the case of an object parameter, if the argument is in the form of an array, such as the parameter arguments is passed in the Apply example, the parameter is an array type, and the list of parameters is consistent when the person is called ( That is, the first two bits of the parameter list of the person and student are Consistent) can be applied, if my Person's parameter list is such (age,name), and student's argument list is (name,age,grade), This can be done with call, that is, directly specify the position of the corresponding value of the parameter list (person.call (this,age,name,grade));<p><p>4, <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--> the package object to ensure that this point</p></p><pre><pre><span style="color: #0000ff;">var</span> <span style="color: #0000ff;"></span> this <span style="color: #000000;">; _this. $box. on (</span><span style="color: #800000;">'</span><span style="color: #800000;">mousedown</span><span style="color: #800000;">'</span><span style="color: #000000;">, function () { </span><span style="color: #0000ff;">return</span> <span style="color: #000000;"> _ This.fndown.call (_this); })</span></pre></pre><p><p>5. Code Optimization</p></p><p><p>Returns the maximum value of an array</p></p><pre>Alert (math.max (<span style="color: #800080;"><span style="color: #800080;">5</span></span>,<span style="color: #800080;"><span style="color: #800080;">8</span></span>))<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">8</span></span>Alert (math.max (<span style="color: #800080;"><span style="color: #800080;">5</span></span>,<span style="color: #800080;"><span style="color: #800080;">7</span></span>,<span style="color: #800080;"><span style="color: #800080;">9</span></span>,<span style="color: #800080;"><span style="color: #800080;">3</span></span>,<span style="color: #800080;"><span style="color: #800080;">1</span></span>,<span style="color: #800080;"><span style="color: #800080;">6</span></span>))<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">9</span></span>Alert (math.max ([<span style="color: #800080;"><span style="color: #800080;">5</span></span>,<span style="color: #800080;"><span style="color: #800080;">7</span></span>,<span style="color: #800080;"><span style="color: #800080;">9</span></span>,<span style="color: #800080;"><span style="color: #800080;">1</span></span>]))<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">find the largest element in the array, which is not possible. </span></span><span style="color: #000000;"><span style="color: #000000;">function Getmax (arr) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>arrlen=<span style="color: #000000;"><span style="color: #000000;">arr.length; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>I=<span style="color: #800080;"><span style="color: #800080;">0</span></span>, ret=arr[<span style="color: #800080;"><span style="color: #800080;">0</span></span>];i<arrlen;i++<span style="color: #000000;"><span style="color: #000000;">) {ret</span></span>=<span style="color: #000000;"><span style="color: #000000;">Math.max (ret,arr[i]); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">ret;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">it's troublesome and inefficient to Write. What if I use apply?</span></span><span style="color: #000000;"><span style="color: #000000;">function GetMax2 (arr) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Math.max.apply (<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">, arr);}</span></span></pre><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--></p></p>The two pieces of code achieve the same goal, but GetMax2 is elegant, efficient, and much simpler. Apply converts a number assembly to a parameter that is passed to the Method. This block in the call when the first parameter gave a null, this is because there is no object to call this method, I just need to use this method to help me to calculate the return of the results on the LINE. so I passed a null past DIRECTLY.<pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">two array splicing, to expand the arr2, and then one by one appended to the arr1</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>arr1=[<span style="color: #800080;"><span style="color: #800080;">1</span></span>,<span style="color: #800080;"><span style="color: #800080;">3</span></span>,<span style="color: #800080;"><span style="color: #800080;">4</span></span><span style="color: #000000;"><span style="color: #000000;">];</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>arr2=[<span style="color: #800080;"><span style="color: #800080;">3</span></span>,<span style="color: #800080;"><span style="color: #800080;">4</span></span>,<span style="color: #800080;"><span style="color: #800080;">5</span></span><span style="color: #000000;"><span style="color: #000000;">];arr1.push (arr2)</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[1,3,4,[3,4,5]] obviously not</span> .</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">can <span style="color: #008000;">only use one loop to go to one push (can also use Arr1.concat (arr2), but the concat method does not change arr1 itself)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>arrlen=<span style="color: #000000;"><span style="color: #000000;">Arr2.length</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>I=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<arrlen;i++<span style="color: #000000;"><span style="color: #000000;">) {arr1.push (arr2[i]);}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">The push method is executed using apply,arr1, and ARR2 is passed in as a parameter. ARR1 invokes the push method, which is a set of parameters that are assembled into a parameter list by apply</span></span>Array.prototype.push.apply (arr1,arr2)</pre><p><p> <!--?xml version="1.0" encoding="UTF-8" standalone="no"?--></p></p>Both call and apply are intended to change the context in which a function is run, in other words, to change the direction of this within the function Body. Because JavaScript functions have a concept of "context at definition" and "runtime context" and "context can be changed."<em id="__mceDel"><em id="__mceDel">the two work exactly the same way, except that they accept the parameters differently.</em></em><pre><pre>function add (a, b) {console.dir (<span style="color: #0000ff;"></span>this<span style="color: #000000;">);} function Sub (a, b) {console.dir (</span><span style="color: #0000ff;"></span>this<span style="color: #000000;">);} Add (</span><span style="color: #800080;">1</span>,<span style="color: #800080;">2</span><span style="color: #008000;">//</span><span style="color: #008000;">"window</span>" Sub (<span style="color: #800080;">1</span>,<span style="color: #800080;">2</span><span style="color: #008000;">//</span><span style="color: #008000;">"window" </span> <span style="color: #800080;">1</span><span style="color: #800080;">2</span><span style="color: #008000;">//</span><span style="color: #008000;">"sub (a, b)"</span>sub.apply (add, [<span style="color: #800080;">1</span><span style="color: #800080;">2</span><span style="color: #008000;">// </span> <span style="color: #008000;">"add (a, b)"</span></pre></pre><p><p></p></p><p><p> JavaScript inheritance mechanism & call apply instructions </p> </p></span>

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.