Object concept--javascript Object-oriented advanced

Source: Internet
Author: User

What is an object?

A. Abstraction of real things-object- oriented is the abstraction of real things
B. Encapsulation of data-a package of specific data, a collection of properties and methods

Three major features of an object (encapsulation, inheritance, polymorphism)

1. Encapsulation is the encapsulation of properties and methods
2. Inheritance is what others do not have, take over to become their own is inheritance, JS does not have a specific inheritance mechanism, but can be technically practical
3. Polymorphism, the different manifestations of different situations. If a method, pass a parameter and pass multiple parameters, get the result is not the same

# # Four ways to create an object # #
* * 1. Declaration Paradigm * *
var obj = {}

**2. Factory function 1**
function Stu (name,age) {
return {
Name:name,
Age:age,
Sayhello:function () {
Console.log (' I am ' +this.name+ ' this year ' +this.age+ ' years ')
//         }
//       }
//    }

var stu1 = stu (' Zhangsan ', 21)
Stu1.sayhello ()
//
**3. Factory function 2**
function Stu (name,age) {
var obj = {};
Obj.name = name;
Obj.age = age,
Obj.sayhello = function () {
Console.log (' I am ' +this.name+ ' this year ' +this.age+ ' years ')
//      }
return obj
//    }
var stu1 = stu (' lis ', 22);
Stu1.sayhello ()
//
//
**4. Factory function 3--Constructor, key * *
      //function Student (name,age) {
      //   this.name = name;
      //   this.age = age;
      //   this.sayhello = function () {
      //       Console.log (' I am ' +this.name+ ' this year ' +this.age+ ')
       //   }
      //}
      //var stu1 = new Student (' Wang5 ', 1);
      //Stu1.sayhello ()

Object concept--javascript Object-oriented advanced

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.