Liaoche's official website "JavaScript Tutorial-Methods" Personal summary

Source: Internet
Author: User

3.3. Methods

Define object var xiaoming = {name: "Xiaoming", brith:1990}//bind a function to an object var xiaoming = {name: "Xiaoming", brith:1990, Age:functio        N () {var y = new Date (). getFullYear ();    return y-this.brith; }}//functionxiaoming.age;//26xiaoming.age ();

Inside a method, this is a special variable that always points to the current object, that is, the xiaoming variable. So, This.birth can get Xiaoming's birth attribute.

function Getage () {var y = new Date (). getFullYear (); return y-this.brith;} Define object var xiaoming = {name: "Xiaoming", brith:1990}//bind a function to an object var xiaoming = {name: "Xiaoming", brith:1990, Age:get Age}//functionxiaoming.age;//26xiaoming.age ();

Call function Getage () How did you return nan? Please note that we have entered a big hole in JavaScript.

If this is called inside the function of JavaScript, who is this?

The answer is, depending on the situation!

If called as a method of an object, such as Xiaoming.age (), this function's this point points to the called object, which is xiaoming, which is what we expected.

If a function is called separately, such as Getage (), this point of the function points to the global object, which is window.

 () 

> No, too! To ensure that this Pointing correctly, you must use obj.xxx () the form call!

fn = Xiaoming.fn ()

Since this is a huge design error, it's not that simple to correct. The ECMA decision, in strict mode, allows the function'sthisPointingundefined, so, in strict mode, you get an error:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/8C/3A/wKioL1hmABzgQsGUAAAWWBmJMx8578.png-wh_500x0-wm_3 -wmp_4-s_564864968.png "title=" Qq20161230142911.png "alt=" Wkiol1hmabzgqsguaaawwbmjmx8578.png-wh_50 "/>

The result is another error! Reason isthisthe pointer is onlyagemethod within the function of thexiaoming, a function defined inside a function,thisalso point toundefinedout! (in non-strict mode, it points back to the global objectwindow! )

Xiaoming = {:::() {() {year = Date (). ()        Year-.} ()}}xiaoming. ()

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8C/3B/wKioL1hmBwGznlOJAAAWuJDGeAU298.png-wh_500x0-wm_3 -wmp_4-s_565428758.png "title=" Qq20161230150411.png "alt=" Wkiol1hmbwgznlojaaawujdgeau298.png-wh_50 "/>

> There is no way to fix it, we use a that variables first capture this :

Xiaoming = {:::() {that = () {year = Date (). ()        Year-that.} ()}}xiaoming. ()

with var that = this; , you can safely define other functions within the method, rather than piling all the statements into a single method.

Apply

Although in a separate function call, depending on whether it is the strict mode,thisPointingundefinedOrwindow, however, we can still controlthisThe point of the!

To specify the function'sthisThe object to which you are pointing, you can use the function itselfapplymethod, which receives two parameters, the first parameter is the one that needs to be boundthisVariable, the second parameter isArrayA parameter that represents the function itself.

applyCall with repair getAge() :

() {Date (). () - .} Xiaoming = {:::}xiaoming. (). (xiaoming,[])

apply () Similar methods are call () , the only difference is:

  • apply()Package the parameters into a Array re-incoming;

  • call()The parameters are passed in order.

such as calls Math.max(3, 5, 4) , respectively, apply() and call() implemented as follows:

.. ([]).. ()

for normal function calls, we usually put this bound to null

Decorative Device

Using apply() , we can also dynamically change the behavior of the function.

All JavaScript objects are dynamic, even if built-in functions, we can also re-point to the new function.

parseint () , you can find all the calls, and then manually add count + = 1 , but it's foolish to do so. The best solution is to replace the default parseint () :

Count = Oldparseint =. = () {Count++oldparseint. ( Arguments)} (()) (()) (()) (count)



Liaoche's official website "JavaScript Tutorial-Methods" Personal summary

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.