Create an object using JavaScript

Source: Internet
Author: User

JavaScript has built-in objects such as Date, Array, and String, which are powerful and easy to use and can be seen and loved. However, when dealing with complicated logic, the built-in objects are very weak, developers often need custom objects. What is an object? In JavaScript definition, an object is a set of unordered attributes. Its Attributes can contain basic values, objects, or functions. That is to say, an object is a group of attributes with no specific order. Each attribute is mapped to a value, which is a group of key-value pairs. The value can be data or an object. A pair of braces {} of the simplest Object JavaScript can define an Object. In fact, this method is the same as calling the Object constructor var obj = {}; var obj2 = new Object (); in this way, the constructed Object only contains a pointer to the prototype of the Object. Some valueOf, hasQwnProperty, and other methods can be used, which has little practical effect, custom objects always have custom attributes and methods. Var obj = {}; obj. a = 0; obj. fn = function () {alert (this);} var obj2 = {a: 0, fn: function () {alert (this );}} after defining the object, you can use ". you can also use the literal Assignment Method to add attributes and methods to an object when defining it, the method and attributes can be directly referenced by objects, which are similar to static variables and static functions of classes. In this way, creating an object has a major defect-it is very laborious to define a large number of objects, writing code over and over again is almost repetitive. Abstract: Since the code is repeated, You can abstract it and use functions to do the repetitive work. When creating an object, you can call a method dedicated to object creation, for different attribute values, you only need to input different parameters. Function createObj (a, fn) {var obj = {}; obj. a = a; obj. fn = fn; return obj;} var obj = createObj (2, function () {alert (this. a) ;}); in this way, when creating a large number of objects, you can call this method to do some repetitive work. This method is not perfect, in many cases, you need to determine the Object type. The objects created by the code above are all original Object instances, but some attributes and methods are extended. When there are some functions, function is an object in JavaScript. When creating an object, you can leave the createObj method above and directly use function as an object. How can we achieve reuse, this lies in the particularity of function as an object. 1. function can accept parameters. You can create objects of the same type and different values based on the parameters. 2. when a function is called as a constructor (called by the new operator), an object is returned. In the poor Chinese jQuery, the basic knowledge of constructor is mentioned, A simple copy of the return value of a constructor is divided into two situations. When a function does not have a return statement or returns a basic type (bool, int, string, undefined, null, returns an anonymous Object created by new, which is a function instance. If the Function returns a reference type Object (such as Array, function, Object, this object will overwrite the new anonymous object as the return value. 3. so how does function solve the type recognition problem? Every function instance object has a constructor attribute (not "yes", but can correspond to it ), this attribute can indicate who the object is constructed, or use the instanceof operator to determine whether the object is an instance of XXX. You can't just say you don't want to practice it. Go to the code function Person (name) {this. name = name; this. fn = function () {alert (this. name) ;}} var person1 = new Person ('byron '); console. log (person1.constructor = Person); // true console. log (person1 instanceof Person); // true is perfect, neither! Although constructors can be object-specific, the methods in each instance of the object must be repeated! Function Person (name) {this. name = name; this. fn = function () {alert (this. name) ;}} var person1 = new Person ('byron '); var person2 = new Person ('frank'); console. log (person1.fn = person2.fn); // false. Although the fn of the two instances are identical, this is not the same thing. If a function object has one thousand methods, therefore, each instance must contain the copy of these methods, which makes the memory speechless. Is there a almost perfect way to construct objects without repeated work or type, and there is no need to repeat Common Object methods? In fact, it can be found that the use of the function is already close to the requirement, just a little bit worse-the container that needs to be shared by an instance of all function objects, the attributes and methods that need to be shared in the memory of this container, this container is a ready-made prototype. If you are not familiar with prototype, you can check JavaScript prototype function Person (name) {this. name = name;} Person. prototype. share = []; Person. prototype. printName = function () {alert (this. name);} var person1 = new Person ('byron '); var person2 = new Person ('frank'); console. log (person1.printName = person2.printName); // true Son instances all have their own attribute names, and all share attributes shared by instances and printName methods. The basic problem is solved, for general object processing, you can always create an object mode in a stylish and loving manner.

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.