JavaScript Writing Method (3)

Source: Internet
Author: User

Part 1,

Constructor + prototype assemble a class. The same constructor can define multiple types.


View sourceprint? 01 /**

02 * $ class write tool function 2

03 * @ param {Function} constructor

04 * @ param {Object} prototype

05 */

06 function $ class (constructor, prototype ){

07 var c = constructor | function (){};

08 var p = prototype | | {};

09 return function (){

10 for (var recognition in p ){

11 arguments. callee. prototype [recognition] = p [recognition];

12}

13 c. apply (this, arguments );

14}

15}


Similar to the previous article, constructors and prototype objects are still used to define two classes.

View sourceprint? 01 // Constructor

02 function Person (name ){

03 this. name = name;

04}

05 // prototype object

06 var proto = {

07 getName: function () {return this. name },

08 setName: function (name) {this. name = name ;}

09}

10 // write two classes

11 var Man = $ class (Person, proto );

12 var Woman = $ class (Person, proto );

Unlike the previous article, although Man and Woman are both assembled using Person and proto. But Man is not equal to Woman. That is, the same Constructor (Person) defines different classes.

View sourceprint? 1 // The same Constructor (Person) defines different classes

2 console. log (Man = Woman); // false

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.