JavaScript Note 3-Object-oriented programming-creating objects

Source: Internet
Author: User

JavaScript objects: A collection of unordered properties, each of which can contain basic values, objects, or functions. Example:

1  var person = new Object (); 2         person.name = "Nicholas"; 3         person.age = 29;4         person.job = "Software Enginee R "; 5         person.sayname = function () {6             alert (this.name); 7         };8         9         person.sayname ();

The person is an object, and name, age, job, Sayname are all properties of the object. Where Sayname is a special property, it has a function.

In fact, a function is also a type, a reference type, and an object is a reference type, so you can re-describe the JavaScript object: The object has properties, and each property can contain the base type and the reference type.

  1. Object is created:
    • Create an object instance, and then add the properties:
      1  var person = new Object (); 2         person.name = "Nicholas"; 3         person.age = 29;4         person.job = "Software Enginee R "; 5         person.sayname = function () {6             alert (this.name); 7         };8         9         person.sayname ();

    • Direct use of literal:
      var person={  Name: "JAY",  age:29,  job: ' singer ',  sayname:function () {   alert (this.name);}    };

  2. policies for creating objects
    • Factory mode (encapsulates the process of creating objects to make the code neater):
       function Createperson (name, age, Job) {            var o = new Object ();            O.name = name;            O.age = age;            O.job = job;            O.sayname = function () {alert (this.name);                };        return o;        } var = createperson ("Nicholas", Person1, "Software Engineer"); var person2 = Createperson ("Greg", "Doctor"); 

      Disadvantage: The object is created, but if you ask what type of Person1, Person2, is not recognized. Workaround: Constructor mode.

    • Constructor mode:
      function person (name, age, Job) {            this.name = name;            This.age = age;            This.job = job;            This.sayname = function () {                alert (this.name);            };            }                var person1 = new Person ("Nicholas", "Software Engineer");        var person2 = new Person ("Greg", "Doctor");
      • Pros: You can know the type of object (person).
        Constructors are also functions. Any function using new becomes a constructor (except that the first letter of the conventional constructor is capitalized), without new being the general function.
      • Cons: Sayname is a function (a function is an object, a reference type), and each time new person is created, the instance of Sayname is recreated. Workaround: Prototype mode.
    • Prototype mode (instance sharing properties and methods)

      Attributes are added when the function is created, and each function has a hidden property: Prototype (prototype).
      Prototype is a pointer to an object that contains some properties and methods shared by all instances.

      function person () {        }                Person.prototype.name = "Nicholas";        Person.prototype.age =;        Person.prototype.job = "Software Engineer";        Person.prototype.sayName = function () {            alert (this.name);        };                var person1 = new Person ();        Person1.sayname ();   "Nicholas"                var person2 = new Person ();        Person2.sayname ();   "Nicholas"              alert (person1.sayname = = person2.sayname);  True

      Very similar to the parent class. At this point, Person1 and Person2 's sayname are the same reference.

      • When you create a new function, a, the prototype property is automatically created for the function. This property points to the prototype object B of the function.
      • B has a constructor property that contains a pointer to a.

      • The prototype relationship is determined by b.isprototypeof (Person1).
      • The
      • cannot modify the property values of a prototype through an object instance.
         function person () {} Person.prototype.name = "Nicholas";        Person.prototype.age = 29;        Person.prototype.job = "Software Engineer";        Person.prototype.sayName = function () {alert (this.name);                };        var person1 = new Person ();                var person2 = new Person ();        Person1.name = "Greg";   alert (person1.name);   "Greg"? from Instance alert (Person2.name); "Nicholas"? From prototype 

         

      • Simpler prototype syntax:
        function person () {        }        person.prototype = {            name: "Nicholas",            age:29,            job: "Software Engineer",            sayname:function () {                alert (this.name);            }};

        In this way, the constructor property no longer points to person. (because creating a function generates a prototype property that points to the address of the prototype object, the constructor property of the prototype object points to person, and in this way, prototype is overridden, The resulting prototype object B is not an original prototype object, so the constructor of B will no longer point to a.

JavaScript Note 3-Object-oriented programming-creating objects

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.