Javascript notes and summaries (1-4) this

Source: Internet
Author: User

4 ways to call functions in JS:

① is called as a normal function, this value points to window, which, to be exact, is null and is interpreted as window. In the ECMASCRIPT5 standard, if this is null, it is interpreted as undefined

<script>function  t () {    this. x = ' Hello';} T (); alert (window.x); </script>

Popup: Hello

②a. Called as the object's method, this points to the caller of the method, that is, the object

<script>var obj = {x: ' hello ', y: ' World ', T:function() {alert (this. x)}};o BJ.T (); </script>

Popup: Hello

B.

<script>var obj = {x: ' hello ', y: ' World ', T:function() {alert (this. x)}}; var dog = {x: ' Wang '= obj.t;dog.t (); </script>

Pop up: Wang

When called as a method,this points to the caller of the moment it was called , the parent object, regardless of the called function, which is declared as a method or function

C.

<script>var obj = {x: ' hello ', y: ' World ', T:function() {alert (this. x)}}; var dog = {x: ' Wang '=function() {    alert (' show ' + this  = show;dog.t (); </script>

D.

<script>var obj = {x: ' hello ', y: ' World ', T:function() {alert (this. x)}}; var dog = {x: ' Wang '=function() {    alert (' show ' + this  = show;dog.t (); </script>

Pop up: Show Wang

③ as a constructor

JS does not have the concept of class, create objects using constructors to complete, or directly in JSON format {} to write objects

<script>function  Dog (name,age) {    this. Name = name;      this. Age = Age ;      This function () {        alert (this. Name);    }} var New Dog (' Niu ', 2);d Og.bark (); </script>

Pop Up: Girl

New Dog takes a few steps:

A. System creates empty object {}, (empty object construct property points to the Dog function)

B. Point the This function to the empty object

C. Execute the function

D. Return the object

Cases:

<script>function  Robit () {    this. Name = ' Watt Force ';      return ' Wali ';} var New Robit (); Console.log (Robit); </script>

This code will output?

Answer:

Robit {name: "Watts"}

The output is a Robit object, because when the function is run as a constructor, the return value is ignored and returns the object (no meaning).

The ④ function is called by call,apply

Syntax format: function. Call (object, parameter 1, parameter 2 ...) Parameter n);

<script>function  t (num) {    alert (' my age is ' + this');    Alert (' Although I look like ' + '(this. age+num);} var = {Name: ' Diego Human ', age:27= t;human.t (ten); </script>

Pop-up: my age is 27

Pop up: Though I look like 28

Next, do not assign t as human attribute, can also point this to human

<script>function  t (num) {    alert (' my age is ' + this');    Alert (' Although I look like ' + '(this. age+num);} var giroud = {name: ' Giroud ', age:28};t.call (Giroud,-5); </script>

Pop-up: my age is 28

Pop up: Though I look like 23

"Analysis":

Fn.call (object, parameter 1, parameter 2 ...) Parameter n);

Run as follows:

A. The This in the FN function points to the object obj

B. Run fn (parameter 1, parameter 2 ...). Parameter n)

Javascript notes and summaries (1-4) this

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.