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'sthis
Pointingundefined
, 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 isthis
the pointer is onlyage
method within the function of thexiaoming
, a function defined inside a function,this
also point toundefined
out! (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,this
Pointingundefined
Orwindow
, however, we can still controlthis
The point of the!
To specify the function'sthis
The object to which you are pointing, you can use the function itselfapply
method, which receives two parameters, the first parameter is the one that needs to be boundthis
Variable, the second parameter isArray
A parameter that represents the function itself.
apply
Call with repair getAge()
:
() {Date (). () - .} Xiaoming = {:::}xiaoming. (). (xiaoming,[])
apply () Similar methods are call ()
, the only difference is:
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