JS Create object

Source: Internet
Author: User

ECMA-262 defines an object that is a collection of unordered attributes, whose properties can contain basic values, objects, or functions

It's plain to wrap a specific set of variables and methods.

So let's start by creating an object

var person=new Object ();p erson.name= "Sakura";p erson.age=22;p erson.job= "front-end development";p erson.sayname= function() {Console.log (this. name);} //When a variable is encapsulated in an object, this variable can be called an attribute of the object ///The same, when a function is encapsulated in an object, this function can be called a method of the object  

In fact, this is an old way of writing, so we can rewrite it with the object literal.

var person={  this.name= "Sakura",  this.age=22this.job= "front-end development"This.sayname=  function() {Console.log (this. name);}}         

Such an object is created, and when we need to invoke a method of an object, or get a property of an object, we can use "." Number to invoke

?
1234 vara=person.name;    //获取对象的属性 console.log(a);        //Sakura      person.sayName();    //调用对象的方法

In general, objects created in this way can meet some of our needs in some cases, but the fact is that we may need to create a large number of objects with some of the same characteristics (attributes or methods) in the actual work.

This involves the reusability of the code, after all, no one wants to repeat things over and over again, so smart handlers invent several reusable object creation methods. "

1. Factory mode

Factory mode abstracts The process of creating a specific object, which is actually a function that encapsulates a specific interface to create an object

function Createpeson (name,age,job) {  var obj=new Object ();  Obj.name=name, obj.age= age, obj.job=job, obj.sayname=function() {Console.log ( this Note that this here represents obj, not Createpersonreturn obj;          

This Createperson is a factory method that receives three parameters, creates a new object internally, initializes the properties and methods, and then returns the object

var Person_01=createperson ("Sakura", 22, "front-end development");  var Person_02=createperson ("Misaka", 20, "web Design"); 

This way we can create more different objects without having to write too much redundant code, just with a factory method.

There's everything. However, before we used the instanceof operator to determine the type of the object, our code was like this

var abc=function(num) {  return num+1/////function type Object     

We notice that the ABC function is both an object and a function, so we can assume that the functions ABC is an object of type

Similarly, an array is an object of type array

But what are the person_01 we created above?

Yes, he's just an object, if we need to create a lot of factory patterns in our work and create many different kinds of objects based on these factory methods, how can we tell what type of objects they are or which factory methods are created?

Apparently instanceof can't tell us the answer.

JS Create 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.