Home > Web Front-end > JS Tutorial > body text

What are JavaScript objects? Detailed explanation of how to create object code

伊谢尔伦
Release: 2017-07-24 15:36:36
Original
1315 people have browsed it

What is an object

According to the definition of JavaScript, an object is a collection of unordered properties, and its properties can contain basic values, objects or functions. That is to say, an object is a set of attributes in no particular order. Each attribute is mapped to a value, which is a set of key-value pairs. The value can be data or an object.

The simplest object
A pair of curly brackets {} in JavaScript can define an object. This way of writing is actually the same as calling the constructor of Object

var obj={};
var obj2=new Object();
Copy after login

It is constructed in this way The object only contains a pointer to the prototype of the Object. You can use some methods such as valueOf and hasQwnProperty, which have little practical effect. Custom objects always have some custom properties and methods.

var obj={};
            obj.a=0;
            obj.fn=function(){
                alert(this);
            }
            var obj2={
                a:0,
                fn:function(){
                    alert(this);
                }
            }
Copy after login

You can add properties and methods to it through "." after defining the object, or you can use the literal assignment method to add properties and methods to it when defining the object. The object created in this way has its methods. And attributes can directly use object references, similar to static variables and static functions of classes. There is an obvious flaw in creating objects in this way - it is very laborious to define a large number of objects, and it is necessary to write almost repetitive code over and over again.

It’s time for function to appear again. In JavaScript, function is an object. When creating an object, you can discard the createObj method above and directly use function as an object. How to achieve reuse? This lies in function as an object. The particularity of the object.

1. function can accept parameters, and can create objects of the same type and different values ​​​​according to the parameters

2. When function is used as a constructor (called through the new operator), it will return an object. Some basic knowledge about constructors has been mentioned in the poor and lower-middle peasant version of jQuery. Let’s simply copy it.

The return value of the constructor is divided into two situations. When the function has no return statement or returns a basic type (bool, int, string, undefined, null), return an anonymous object created by new, which is the function instance; if the function body returns a reference type object (Array, Function, Object, etc.), the object will overwrite the creation of new An anonymous object as the return value.

3. So how to use function to solve the problem of type identification? Each function instance object will have a constructor attribute (not "has", but can correspond). This attribute can indicate who its constructor is. , you can also use the instanceof operator to determine whether the object is an instance of XXX.

The code is as follows:

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
Copy after login

This would be perfect, no! Although the constructor can be object-specific, the methods must be repeated in every instance of the object!

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
Copy after login

Look, although the fn of the two instances are exactly the same, they are not the same thing. If a function object has a thousand methods, then each of its instances must contain these methods. copy, it makes the memory speechless.

Is there a nearly perfect way to construct an object that does not require repeated work, is stylish, and does not need to repeat the common methods of objects? In fact, it can be found that using function is already close to the requirements and is just a little short of it - it requires a container shared by all instances of the function object, and the shared attributes and methods that need to be shared by the instance in this container happen to be ready-made - —prototype, students who don’t know prototype can look at 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
Copy after login

In this way, each instance of Person has its own attribute name, and also has the attribute share and method printName shared by all instances. The basic problems are solved. , for general object processing, you can always use this stylish and loving object creation mode.

The above is the detailed content of What are JavaScript objects? Detailed explanation of how to create object code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!