The This in JavaScript

Source: Internet
Author: User

The article is not very large, most of the content is I refer to other people's blog after their own understanding of the arrangement. Convenient for you to review knowledge later. The text refers to the slice (), call (), and apply () of the array object in JavaScript.

Slice () method, in the project I did before, there are two types of data used. The first is a string type, and the other is an array object;

Slice () of the string object:

Definition and usage

The slice () method extracts a portion of a string and returns the extracted part with a new string.

Grammar
Stringobject.slice (Start,end)
Parameters Description
Start The starting subscript for the fragment to be extracted. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.
End The subscript immediately following the end of the fragment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.
return value

A new string. Includes the string Stringobject all characters from start (including start) to end (not including end).

Slice () of the array object:

Definition and usage

The slice () method returns the selected element from an existing array.

Grammar
Arrayobject.slice (Start,end)
Parameters Description
Start Necessary. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
End Optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array.
return value

Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.

Hints and Notes

Note: You can use negative values to select elements from the end of an array.

Note: If end is not specified, then the slice () method selects all elements from start to the end of the array.

The call () and apply () methods use the description:

Each function contains two non-inherited methods apply and call

They have the same purpose, and they all call functions in a specific scope.

Receive parameters, apply receives two parameters, the first parameter is this the second parameter is an array, the first parameter of the call method is the same as the Apply parameter, but the parameters passed to the function must be listed.

In JavaScript, this is some one-sided, not the whole place of their own follow-up will be organized.

  

1 /*!2 * Package Console.log () method, convenient to call later;3 */4 functionlog () {5     varinfo = Array.prototype.slice.call (arguments,0);6 console.log.apply (console,info);7 }8 /*!9 * Usage of thisTen */ One  A /*! - * If it is a global function, this is equivalent to the Window object, - * The various properties and methods defined by this in the function can be accessed outside the function. the * If this function needs to be called (if not called, then the methods and properties defined inside will not be taken); - */ - functionGLOBALFN () { -      This. name= "Window";  + } -Log This);//Window {top:window, Window:window, Location:location, External:object, Chrome:object ...} +Log This. name);//"" A  at GLOBALFN (); -Log This. name);//"Window" -  - /*! - * If an object is instantiated with the new method, this is not equal to an instance of Window,this pointing to function; - */ in functionAnimal () { -      This. type= "Animal"; to } + varAnimal0 =NewAnimal (); -Log (animal0);//Animal {type: "Animal"} the //Animal0.song (); Uncaught typeerror:undefined is not a function *  $ /*!Panax Notoginseng * Using the prototype extension method, you can use this to obtain an instance of the source object, and the private field cannot be obtained through the prototype chain; - */ theAnimal.prototype = { +Songfunction(){ AConsole.log ( This. type+ "Song ..."); the     } + } - varAnimal1 =NewAnimal (); $Log (ANIMAL1);//Animal {type: "Animal", song:function} $  - /*! - * Either directly referencing function or instantiating a function, the enclosing function returned by this is pointing to window; the */ - functionOuterfn () {Wuyi     varname = "outer"; the     functionInnerfn () { -Log This. Name); Wu     } - INNERFN (); About } $OUTERFN ();//window -  - /* - function Outerfn () { A var name = "outer"; + function Innerfn () { the log (this.name); -     } $ return INNERFN; the } the var outer = new OUTERFN (); the outer ();//window; the */

The This in JavaScript

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.