JavaScript The simple types of are Numbers 、 character string 、 Boolean value 、null value 、undefined value .

Other values are objects . Objects are variable keying sets . Array 、 function 、 Regular expressions are objects .

Objects are containers for properties , Attributes are name and value pairs . Names can be all strings including empty strings . The value can be in addition to undefined Any value other than .

JavaScript Objects in are unclassified (class-free) Of .

JavaScript Including a prototype chain feature , Allow an object to inherit the properties of another object . Correct use can reduce the time and memory consumption of object initialization .

Object literal :

var empty_object = {};
var stoog = {
"first-name" : "Jerry",
second_name : "howead"
};

Be careful :
1. If the property name is legal and not a reserved word , What's the name   quotes   Don't write . in other words , yes “ Reserved words ” When ,“ The attribute name ” Be sure to quote , otherwise IE browsers Report errors ;
2. in addition , Object's last property value is followed by no comma “,” Of , Otherwise ,IE The browser will report an error again , Bear in mind !!!

retrieval :

1、stoog["first-name"]

2、stoog.second_name

If you try to return the value of a nonexistent element , Then it will return a undefined value .

|| Can be used to fill in the default values

Such as :var middle = stoog["first-name"]||"(none)";

retrieval undefined The value will cause TypeError abnormal , have access to && To avoid mistakes

flight.equipment && flight.equipment.model    //undefined.

quote reference

Object is passed by reference , They will never be copied

Prototype Prototype

When used Prototype After writing a class , When new A new object, The browser will automatically put prototype The content in is attached to object On .

Reflection Reflection

typeof flight.number //'number' This method checks the prototype chain .

flight.hasOwnProperty('number') //true . This method does not check the prototype chain

enumeration Enumeration

for in It can be used to traverse all property names in an object . Will list all the attributes , Include functions and properties in prototypes that you may not care about . And not in order .

So it can be used typeof perhaps hasOwnProperty To filter out unwanted values .

So we usually use for instead of for in, We can get the attributes we want , Instead of getting the attributes in the prototype chain , And it can be obtained in order .

Delete delete

It removes the properties contained in the object , But don't touch any objects in the prototype chain . So deleting an object's attributes may make the attributes in the prototype chain emerge .

Reduce global variable pollution Global Abatement

One way to minimize the use of all variables is to create only one global variable in the application , Then let that variable be called your application's container .

Closures are also an effective way to reduce global pollution .


JavaScript Literal object And   Array A mixture of

1、 Array creation

1.1 use “ Array literal ”

JS The array of is typeless , It can store any type of data , Such as :

var aValues=["string", 24, true, null];

Here we define an array literal quantity , It can be accessed as follows :

alert(aValues[0]); // Output string ”string”
alert(aValues[1]); // Output number 24
alert(aValues[2]); // Output Boolean values true
alert(aValues[3]); // Output null.

1.2、 If you don't want to define arrays in literal notation , It can also be used. Array Constructor for . Such as :

var aValues = new Array("string", 24, true, null);

2、 Object creation

2.1、 In terms of objects , Such as :

var oCar = {
"color": "red",
"doors": 4,
"paidfor": true
}

Here you create an object , There are three attributes in it , Available as “.” To access the properties of an object :

alert(oCar.color); // Output ”red”
alert(oCar.door); // Output ”4”
alert(oCar.paidfor); // Output ”true”

Also available “[“ attribute ”]” Get the property value in the same way , as follows :

alert(oCar["color"]); // Output string ”red”
alert(oCar["door"]); // Output string ”4”
alert(oCar["paidfor"]); // Output string ”true”

2.2、 Again , You can also use JS Of object Constructor to create an object . as follows :

var oCar = new object();
oCar.color = "red";
oCar.doors = 4;
oCar.paidfor = true;

2、 A mixture of arrays and objects

You can only use mixed literal quantities here , To create “ An array of objects ” or “ Objects that contain arrays ”. as follows :

var oCars = [
{
"color": "red",
"doors": 2,
"paidfor": true },
{
"color": "blue",
"doors": 4,
"paidfor": true
},
{
"color": "white",
"doors": 2,
"paidfor": false
}
];

This is an array of objects , It contains three objects , It's like this when you visit :

alert(oCars[1].color); // Output “blue”

Here's an object that contains an array :

oCarInfo = {
"availableColors": ["red", "blue", "white"],
"availableDoors": [2, 4]
};

It's like :

alert(oCarsInfo.availableColor[1]); // Output “blue”

Extended reading :JavaScript operation JSON Method summary

JavaScript Understanding of the object And More articles about the relationship between literal objects and arrays

  1. Javascript How to disassemble a literal object

    To put it simply , Literal objects provide a way to build new objects that has always been very convenient , Its format is very simple and easy to read . It's a way that most front-end programmers prefer when building objects . Format : var person = { name : " ...

  2. JSON Application ——PHP Use in json Data format defines the method of literal object

    at present , stay PHP Literal quantity nomenclature is not supported in . All the front-end friends know , stay JS The method of defining an object with literal quantity in can be as follows : var o = { 'name' : 'Tom' , 'url' : 'www.baidu.c ...

  3. JS object 、 The difference between basic types and literal quantities

    Face value : var str1='this is a simple string'; var num1=1.45; var answer1=true; Basic types : var str2=String('thi ...

  4. ES6 - Object extension ( Increase the amount of literal )

    /** * Object extension * * Enhance the literal amount of objects * * solve the problem : Reduce the code */ { /** * 1. Attribute simple representation * Variable foo It's written directly in braces . At this time , The property name is the variable name , Property values are variable values */ ...

  5. char Type pointer and string literal quantity and character array

    1. When one char Type pointer to a literal quantity of a string ( That is, constant strings ) when , The pointer must be written by const modification , otherwise , The system will give deprecated( disapproval ) Warning of . as a result of : The literal amount of a string cannot be changed , When it's a non cons ...

  6. javascript Literal And variables The relationship between

    https://www.w3cschool.cn/javascript/js-syntax.html

  7. JavaScript Object literal

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 【JavaScript】 Dynamic prototyping creates objects || Why can't you literally create a prototype object ?

    var proto = ""; function Person(name, age, job) { this.name = name; this.age = age; this.j ...

  9. Java Constant , Variable , object ( Literal ) stay JVM Memory location in memory

    Java Constant , Variable , object ( Literal ) stay JVM Memory location in memory 2019-02-26 18:13:09 HD243608836  Read the number 540   Collection   more Category column : JAVA jvm   It's been a long time ...

Random recommendation

  1. Ant-style path patterns

    [ Reprint ]http://blog.itpub.net/29959940/viewspace-1385870/ Ant path Matching principle path matching principle (Path Matching) Spring MVC in ...

  2. SQLServer Use of triggers

    establish : create trigger trigger_name on {table_name view_name} {for After Instead of } [ insert, update,d ...

  3. fork、vfork、clone difference

    stay Linux We mainly provide fork.vfork.clone Three process creation methods . problem stay linux The execution process of these three calls in the source code is execution fork(),vfork(),clone() when , Map to through a system call table s ...

  4. php And so on , object ( Two ) inheritance ,static Static ,const Constant

    The three major characteristics The second   inheritance : 1. Concept : If a class has subclasses , Then the subclass inherits everything from the parent , But private members don't have access to . 2. You need to add keywords when defining subclasses :extends class Text extends Info ...

  5. 【 turn 】Nutch Of Hadoop Mode crawling efficiency optimization

    Original address :http://my.oschina.net/junfrank/blog/290404

  6. Codility:Titanium 2016 challenge:BracketsRotation

    Find out codility It's hard to find your own code on the Internet , So save it . It's a water method , I don't know whether it's the conclusion or the data . After all the maximal legal strings are processed, only )))(((( The brackets in the figure , Then enumerate the right endpoint , The left end is monotonous . But the number of unmatched points ...

  7. python File download

    To demonstrate urllib3 Use , We are going to download two files from a website here . First , Import required urllib3 library : import urllib3 The source of these two files url by : url1 = 'http://earthqu ...

  8. FINAL Video pre release

    Video address :http://v.youku.com/v_show/id_XMTg0MjMzNDIwNA==.html?spm=a2hzp.8253869.0.0&from=y1.7-2

  9. About gitblit Successful startup , But in the case of Alibaba cloud's inaccessible Internet address

    1. To configure /data/defaults.properties server.httpBindInterface= Just don't fill in the blanks here . # Specify the interface for Je ...

  10. jdk And jre

    1. Definition JRE(Java Runtime Enviroment) yes Java Operating environment . oriented Java Users of the program , Not developers . If you only download and install JRE, Then your system can only run Java Program .JRE It's luck ...