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)