JavaScript Create objects

Source: Internet
Author: User

Creating JavaScript Objects

With JavaScript, you can define and create your own objects.

The first way to create an object is to create it directly from object. (The biggest problem with creating objects in this way is that there are no class constraints, no reuse of objects, and there is no convention that can cause problems in operation.) )Example:

      

var person = new Object ();  Create a new object            person.name = "Leon";             Person.age =;                        Person.say = function () {  to object creation Method                alert (THIS.name + "," + This.age);            }            Person.say ();            alert (person.age);

The second way to create an object: JSON is the object of JS, but it omits the tags in the XML, but instead uses the {} to complete the description of the object.

Instance:

var person = {                name: "Zhang San",//By property Name: property value to represent, different attributes passed, to interval                age:23,                say:function () {                    alert (this.name + "," + t his.age);                } The last attribute cannot be followed,            }            Person.say ();

An array of objects can also be created with JSON

    
var PS = [        {          name: "Leson",          age:25,          say:function () {               alert (this.name + "," + this.age); 
    
     }}         ,        {          name: "Adia",          age:20           },          {          name: "Peros",          age:30           }     ];< C28/>ps[0].say ();
    

The car object is created by way of a factory, an object is created in Createcar, and the corresponding properties and methods are set for the object, and then the object is returned.

Instance:

  

function Createcar (Name,lunzi) {    var car = new Object ();    Car.name = name;    Car.lunzi = age;    Car.say = function () {              alert ("Car name:" +this.name+ ", Wheel:" +this.lunzi)    }     return car;        var car1= createcar ("Cadillac", 4);    var car2= createcar ("Mercedes", 5);    Car1.say ();    Car2.say ();    

Created by constructors, by using the This keyword inside a function to complete the definition of a property when created with a build function.

Instance:

  

function Person (name,age) {     this.name  = name;        This.age  = age;      This.say  = say;}  Set the behavior to global behavior function say () {        alert (this.name+ "," this.age);} Create the object with new person var p1 = new Person ("Abcs", "n"), var p2 = new Person ("Lops", "N"), alert (P1 instanscof person),//constructor by Under the way to detect

Through the creation of prototypes, the use of prototype-based wear allows you to set properties and methods to be proprietary to the person, and cannot be called through window.

Instance:

function person () {}person.prototype.name = "Leon"; Person.prototype.age = 23; Person.prototype.sex = "Man"; Person.prototype.say = function () {    alert (this.name + "," + This.age);} var p1 = new Person ();p 1.say (), var p2 = new Person ();p 2.say ();p 2.name = "KK";p 2.say ();p 1.say ();

  

 

JavaScript Create objects

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.