Js _ object-oriented Basics

Source: Internet
Author: User

JavaScript does not have a class, but it is simulated using a function closure (closure). Similar to the class in C #, it is called an object in js.
[Javascript]
1. function Person (name, age) {// declare an object, similar to a class defined in C #
2. this. Name = name; // Name is dynamically defined.
3. this. Age = age;
4. this. SayHello = function (){
5. alert ("Hello, I am" + this. name + ", I" + this. age + "years old ");
6 .}
7 .}
8. var p1 = new Person ("Tomy", 22); // use new. Otherwise, the function is called. The Person does not return a value. If new is not used, p1 is undefined.
9. p1.SayHello ();
10. p1.Sex = "male"; // dynamically add a gender to p1.
11. alert (p1.Sex );

 

From Click Here

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.