How objects are defined in JavaScript __java

Source: Internet
Author: User

There is no concept of class in JavaScript, only objects. Whether it's a string, a numeric value, an array, or a function, its essence is an object. In addition, JavaScript is allowed to customize objects. What is an object? In short, objects are special data types with properties and methods . Here are some common ways to define objects. 1, based on existing objects to expand its properties and methods

Simply create an object, and then dynamically add properties and methods to it.

Var object=new object ();
Object.name= "Zhangsan";
Object.sayname=function ()
{
    this.name=name;
 }
Object.sayname ("Lisi");      

2. Factory Method

The advantage of the factory approach is that you don't need to expand the methods and attributes each time, and once built, you can generate multiple objects without paying attention to the creation details or even the class names of the specific classes.

function Factory ()
{
    var object = new Object ();
    Object.username = "Zhangsan";
    Object.password = "123";

    Object.get = function ()
    {return
        (this.username+ "," +this.password);
    }
    return object;
}
var object1 = Factory ();     
Object1.get ();
var object2 = Factory ();

Good object creation should satisfy a method that can be shared by multiple objects, while property is private , making the code more abbreviated. You can also rewrite the above program as a factory creation method with parameters. The improved code is as follows:

function get () {return
    (this.username+ "," +this.password);
}

function Factory (Username,password) {
    var object = new Object ();
    Object.username = Username;
    Object.password = password;

    Object.get = get;
    return object;
}

var Object1 = Factory ("Zhangsan", "123");
Object1.get ();
var object2 = Factory ("Lisi", "456");
3. Constructor Mode

When using the constructor method, the JavaScript engine generates an object for us before the first line of code executes, with a hidden return statement that returns the generated object. Therefore, in the function body, there is no need to explicitly declare.

function person () {
    //before executing the first line of code, JS automatically generates an object
    this.username = "Zhangsan";
    This.password = "123";

    This.get = function () {return
        (this.username+ "," +this.password);
    }
    A hidden return statement that returns the generated object to
the

var person = The new person ();
Person.get ();

Similarly, it can be changed to a constructor that can pass arguments. 4, the use of prototype (prototype) mode

Simply using a stereotype to define an object cannot assign an initial value to a property in a constructor, only to change the property value after the object is generated.

function person ()
{
}

Person.prototype.username = "Zhangsan";
Person.prototype.password = "123";
Person.prototype.get = function () {return
    (this.username+ "," +this.password);
}

var person = new person ();
Person.username = "Lisi";
Person.password = "456";
Person.get ();

If you use a prototype object, all of the generated objects share the attributes in the prototype, so that an object changes the property and is reflected in other objects. You can use the prototype + constructor method to solve this problem, so that the properties between the objects do not interfere with each other, sharing the same method between objects. This is also the recommended method.

function person ()
{
    this.username = new Array ();
    This.password = "";
}

Person.prototype.get = function ()
{return
    (this.username+ "," +this.password);
}

var p1 = new Person ();
var p2 = new Person ();

P1.username.push ("Zhangsan");
P1.password = "123";

P2.username.push ("Lisi");
P2.password = "456";

P1.get ();
P2.get ();
5. Dynamic Prototyping mode

In a constructor, let all objects share a method by the amount of flags, and each object has its own properties.

function person ()
{
    this.username = "Zhangsan";
    This.password = "123";

    if (typeof Person.flag = = "undefined")
    {
        Person.prototype.get = function ()
        {return
            (this.username+), "+this.password);
         }
        Person.flag = true;
    }
}
var p1 = new Person ();
var p2 = new Person ();
P1.get ();
P2.get ();
Statement

The above for personal learning process Summary, if there is a better way or other understanding, welcome to leave a message or DMS discussion.

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.