JavaScript Object-oriented-context invoke model

Source: Internet
Author: User
Tags mathematical functions

Context invocation is a more complex object-oriented approach, free from the traditional this limitation. Here are some examples of how arrays can be manipulated to explain how context is invoked.

The context is the environment, that is, the meaning of the custom setting this syntax: 1. Name of the function. Apply (object, [parameter]); 2. The name of the function. Call (object, parameter); Description: 1. The function name is the function itself, and the default this is the global variable 2 when calling with a function. The function name can also be provided by the method, which refers to the current object when the method call is used. 3. After the call is made with apply, neither the function nor the method is valid. Our this, which is determined by the first parameter of apply NOTE: 1. If the function or method does not have this operation, then no matter what the invocation is actually the same. 2. If the function calls Foo (), then it is a bit like foo.apply (window). 3. If the method calls O.method (), then it is a bit like o.method.apply (O).
Second, the parameter problem
 either call or Apply is exactly the same if no arguments are followed (the function has no arguments and the method has no arguments). "Function foo () {console.log (this);} Foo.apply (obj); Foo.call (obj); the use of the first parameter is also a regular 1. If an object is passed in, it is equivalent to setting this in the function as parameter 2. If the parameter is not passed in, or null is passed in. Undefiend, then the equivalent of this is Window ' foo ' (), foo.apply (); foo.apply (null); Foo.call (undefined); ' 3. If the basic type is passed in, then this is a reference to the wrapper type for the base type * number, number* Boolean, boolean* string, String4. When invoking with context, the original function (  method) may have parameters, then this parameter uses the second (nth) parameter in the context call to represent the 
"function foo (num) {console.log (num);} foo.apply (null, [123] ); Equivalent to Foo (123); "
Three, array case
Converting a pseudo-array to a traditional practice of "var a = {};a[0] = ' a ';  a[1] = ' B '; a.length = 2;//Array comes with method concat//syntax: Arr.concat (1, 2, 3, [4, [5]]);//feature does not modify the original array var arr = [];var NEWARR = Arr.concat (a); "' Because A is a pseudo-array, it just looks like an array. So not here, but the Apply method has an attribute that can be used as an array or a pseudo-array as the parameter ' foo.apply ' (obj, pseudo-array); IE8 does not support ' A as the second parameter of apply ' ' var newArr = Array.prototype.concat.apply ([], a) ' to handle an array conversion, which is actually to take the element one by one out to form a new array, where The method involved in this operation can theoretically be 1. Push, unshift2. Slice3. Splicepush method ' Usage: Arr.push (1); Adds this element to the array and returns the number of elements Arr.push (1, 2, 3); Add these three elements to the array in turn, returning the added number var a = {length:0}; Pseudo-array a[a.length++] = ' abc '; a[0] = ' abc '; a.length++;a[a.length++] = ' def ';//Use an empty array to put the elements into the array in one place var arr = [];arr.push (a);  Instead of expanding the element, add the pseudo-array as an element to the array//use apply again to expand the feature arr.push.apply (arr, a) of the pseudo-array, or use apply to expand the properties of the pseudo-array, which is equivalent to Arr.push ( A[0], a[1]) "Slice" Syntax: Arr.slice (index, EndIndex) If the second argument is not passed, then the method will not modify the original array var a = {length:0};a[if it is obtained from the index consistently a.length++] = ' abc '; a[a.length++] = ' def ';//Suppose he is an array,Should be intercepted from 0 to last//if possible, should be A.slice (0)///But he does not have this method//borrow the slice of the array, convert this to this pseudo-array var arr = [];var NEWARR = Arr.slice . apply (A, [0]); "To find the maximum value in the array traditional ' var max = arr[0];for (var i = 1; i < arr.length; i++) {if (arr[i] > Max) {...}} There are many mathematical functions available in the Math object in JS. Math.max (All-in-one) or using apply can expand the properties of the array "var arr = [123456,12345,1234,345345,234,5]; Math.max.apply (null, arr);

JavaScript Object-oriented-context invoke model

Related Article

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.