JavaScript Create object __java

Source: Internet
Author: User
Tags function definition function prototype
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<script type= "Text/javascript" >
1. Create object, add property method to it
var person = new Object ();
Person.name = "Nicholas";
person.age = 29;
Personal.job = "Software Engineer";

Personal.sayname = function () {
alert (this.name);
};

/*
Disadvantage: Creating many objects with an interface produces a lot of duplicate code
*/

2. Factory mode
function createpersonal (name, age, Job) {
var o = new Object ();
O.name = name;
O.age = age;
O.job = job;
O.sayname = function () {
alert (this.name);
};
return o;
}

/*
The factory pattern solves the problem of object similarity, but does not solve the problem of object recognition (how to know the type of an object)
*/

3. Constructor pattern
function person (name, age, Job) {
THIS.name = name;
This.age = age;
This.job = job;
This.sayname = function () {
alert (this.name);
};
}

/*
constructor patterns do not explicitly create objects, assign methods and properties directly to this object, no return statement
To create a new instance of person, you must use the new operator to actually go through 4 steps
A. Creating a new object
B. Assigning a constructor's scope to a new object (this points to this new object)
C. Executing constructors, adding properties for new objects
D. Returning new objects
The object created by the constructor pattern can recognize its type, but each method must be rebuilt on each instance
*/

Create two instances, create two identical methods not necessary, you can move the function definition to the outside of the method
function person (name, age, Job) {
THIS.name = name;
This.age = age;
This.job = job;
This.sayname = Sayname;
}

function Sayname () {
alert (this.name);
}

/*
This solves the problem of two ways to do the same thing, but the function defined on the global scope can only be invoked by an object, and if the object definition requires many parties, many global functions are defined, and the custom reference type is not encapsulated.
*/

4. Prototype mode
function person () {
}

Person.prototype.name = "Nocholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function () {
alert (this.name);
}

/*
Prototype mode all parameters default to get the same value, if the attribute is a reference type, multiple instances affect each other
*/

5. Construction function prototype blending mode
function person (name, age, Job) {
THIS.name = name;
This.age = age;
This.job = job;
}

Person.prototype.sayName = function () {
alert (this.name);
}

/*
Constructors are used to define instance properties, and prototypes are used to define methods and shared properties. The eigenvalue instance will have a copy of its own instance properties.

At the same time, enjoy a reference to the shared method, which is now almost the most appropriate way to create objects.

</script>
</body>
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.