Four invocation modes of JS advanced-function

Source: Internet
Author: User

1, object method call pattern method internal this point to the object of the current caller D

Defining classes (constructors)

function Dog (dogname) {

Create an empty object to let the empty object ==this

THIS.name = Dogname;

this.age = 0;

This.run = function () {

Console.log (THIS.name + ' is running ... ')

}

This is returned by default if the function calls (new) as a constructor and no data is returned.

}

var d= new Dog (' Wangwang ');

D.run ();

2. Constructor call mode new

function Cat () {

THIS.name = "Cat"

This.age = 19;

This.run = function () {

Console.log (THIS.name + ' is running ... ')

}

}

var cat = new Cat (); Constructor call pattern

Cat.run ()//method invocation mode

3. Function call mode

function f (A, B) {

Console.log (A + ' +b)

Console.log (this) window

}

F (2,3)

Exercises

function Dog () {

This.age = 19;

Console.log (This)

}

Dog ()//window function call pattern

var d = new Dog (); D Constructor Call pattern

4.

Four invocation modes of JS advanced-function

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.