JS elevation 6. Object-oriented Programming (2) Creating an object _2 constructor is also a general function

Source: Internet
Author: User

1. The constructor is also a general function , and the following creates a constructor.

var person=function(name,age,job) {        this. name=name;          this. age= age;         this. job=job;         this. sayname=function() {            Console.log (this. Name);        }    }

(1) Call by using the constructor function

1 var person=New person ("xiaoming", +, "Doctor"); 2   Person.sayname (); // xiaoming

(2) using normal function calls

1  Person ("Xiaoming", "Doctor"); 2   Window.sayname (); // xiaoming 3   Sayname (); // xiaoming

2. You can use call () or apply () to invoke the person function in a specific object scope.

1 var o=New  Object (); 2   Person.call (O, "xiaoming", "Doctor"); 3   Person.apply (o,["xiaoming1", "Doctor"]); // xiaoming1 4   O.sayname (); // xiaoming

JS elevation 6. Object-oriented Programming (2) Creating an object _2 constructor is also a general function

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.