JavaScript Learning notes-creating object Design Patterns

Source: Internet
Author: User

<! DOCTYPE html>
<title> Browser Objects </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<script>
<!--Normal mode of creating objects--
var person = new Object ();
Person.name = "Name1";
Person.job = "Job1";
Person.getname = function () {
return this.name;
}
document.write ("Normal mode:" + person.getname () + ' <br> ');
<!--Summary: If you create objects using the normal pattern above, once you have created more objects, you will create code redundancy, such as 40 students in a class must have a new 40 object-->

<!--Create Object Factory mode--
function CreateObject (name,job) {
var o = new Object ();
O.name = name;
O.job = job;
O.getname = function () {
Return this.name
};
return o; Remember to return the object o
}
var person = CreateObject ("FactoryName1", "manage");
var Anotherperson = CreateObject ("FactoryName2", "manage");
document.write ("Factory mode:" + person.getname () + ', ' + anotherperson.getname () + ' <br> ');
<!--Summary: You can find the Factory mode creation object as long as the parameters can be passed, the factory will have a special machine to make products (objects) can be likened to the ordinary model of the factory assembly line workers, And the factory model can be likened to the machine inside the factory. The following Factory mode is improved by customizing the constructor mode--

<!--Create custom constructor patterns for objects--
function Person (name,job) {
THIS.name = name;
This.job = job;
This.getname = function () {
return this.name;
};
}
var person1 = new Person (' milk pull ', ' assist ');
var person2 = new Person (' symplectic ', ' carry ');
document.write ("Custom constructor mode:" + person1.getname () + ', ' + person2.getname () + ' <br> ');
<!--Summary: Note that this mode and Factory mode is still different, 1. Function name the first letter of P is uppercase this is the constructor of the reference Java,c, 2, the constructor does not have a return value, 3, the function is called with the new keyword; 4, Any function that is used by the New keyword is a constructor--
</script>
</body>

JavaScript Learning notes-creating object Design Patterns

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.