Javascript class declaration

Source: Internet
Author: User

Javascript class declaration

Declare function Person (name, age)

{

Num = 5;

This. name = name;

This. age = age;

This.info = function ()

{

Alert ("My information:" + this. name + this. age +"
");

}

}

 

Alert (Person. num );

Var p1 = new Person ();

Alert (p1.info );

Var p2 = new Person ();

Alert (p2.info );

 

 

The variables without this modifier above belong to the class. Similar to static attributes in Java, class names must be called directly.

The variables modified by this below belong to the class Object Attributes and need to be called by the object.

 

Object methods are generally not declared in this way, because each time an object is new, a memory space will be opened up in the memory, which is relatively memory-consuming.

 

Therefore, the class addition method is generally inherited:

Person. prototype. walk = function ()

{

Alert (this. name + "walking slowly ");

}

P1. p2 now has the walk method,

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.