JavaScript object-oriented Programming Tutorials _javascript Tips

Source: Internet
Author: User

Objects in JavaScript are defined as: A collection of unordered attributes that can contain basic values, objects, or functions. You can think of an object as a hash table, a set of name-value pairs (key:value), where the value can be data or a function, and each object is created based on a reference type.

Understanding objects

There are two ways to create objects in the previous blog, one is to create an instance of object, the other is to use the object literal method:

var person = new Object ();
Person.sex = Mans;
Person.name = bluce
person.age =;
Person.sayhi () = function () {
console.log (' Hello world! ');
}

But more of it is in one of the following ways

var person = {
Sex:man,
name: ' Bluce ', age
: '% ',
sayhi:function () {
console.log (' Hello World! ');
}

Creating objects

You can create a single object using both the object constructor and the object literal method, but there are obvious drawbacks: creating many objects with the same interface produces a lot of duplicate code. The common methods of creating objects are Factory mode, constructor model and prototype model.

Here have their own a question: the adoption of AMD specifications, a single JS file, can be seen as a module, can also be said to be a "class", now with JavaScript in this "class" concept a little confused, hope that the following can distinguish the application of the situation.

About JavaScript Object-oriented programming tutorial small series on the introduction to everyone here, I hope to help you!

Let's add JavaScript to the object-oriented design-Factory mode

Factory mode is a well-known design pattern in software engineering, which abstracts the process of creating specific objects, and can encapsulate the details of creating objects with specific interfaces using functions.

This design pattern has been used in Java DAO before, and it is easier to understand.

function Createperson (name,age,sex) {
  var obj = new Object ();
  Obj.name = name;
  Obj.age = age;
  Obj.sex = sex;
  Obj.sayhi () = function () {
    console.log (this.name);
  }//quotes do not leave out, form good habits return
  obj;
}
var person1 = Createperson ("Bluce", "the", "Man");
var person2 = Createperson ("John", "a Man");

Use this function to create a person object that contains the necessary information, based on the parameters you receive. You can call this function countless times and return an object that contains three properties and one method at a time. The factory pattern solves the problem of creating multiple similar objects, but does not solve the problem of object recognition (how to know the type of an object)

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.