zoukankan      html  css  js  c++  java
  • What’s the difference between “{}” and “[]” while declaring a JavaScript array?

    What’s the difference between “{}” and “[]” while declaring a JavaScript array?

    What’s the difference between “{}” and “[]” while declaring a JavaScript array? Normally I declare like

    var a=[];
    

    What is the meaning of declaring the array as var a={}

    回答1

    Nobody seems to be explaining the difference between an array and an object.

    [] is declaring an array.

    {} is declaring an object.

    An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class. In fact, typeof [] === "object" to further show you that an array is an object.

    The additional features consist of a magic .length property that keeps track of the number of items in the array and a whole slew大量 of methods for operating on the array such as .push(), .pop(), .slice(), .splice(), etc... You can see a list of array methods here.

    An object gives you the ability to associate a property name with a value as in:

    var x = {};
    x.foo = 3;
    x["whatever"] = 10;
    console.log(x.foo);      // shows 3
    console.log(x.whatever); // shows 10
    

    Object properties can be accessed either via the x.foo syntax or via the array-like syntax x["foo"]. The advantage of the latter syntax is that you can use a variable as the property name like x[myvar] and using the latter syntax, you can use property names that contain characters that Javascript won't allow in the x.foo syntax.

    A property name can be any string value.


    An array is an object so it has all the same capabilities of an object plus a bunch of additional features for managing an ordered, sequential list of numbered indexes starting from 0 and going up to some length. Arrays are typically used for an ordered list of items that are accessed by numerical index. And, because the array is ordered, there are lots of useful features to manage the order of the list .sort() or to add or remove things from the list.

    回答2

    When you declare

    var a=[];
    

    you are declaring a empty array.

    But when you are declaring

    var a={};
    

    you are declaring a Object .

    Although Array is also Object in Javascript but it is numeric key paired values. Which have all the functionality of object but Added some few method of Array like Push,Splice,Length and so on.

    So if you want Some values where you need to use numeric keys use Array. else use object. you can Create object like:

    var a={name:"abc",age:"14"}; 
    

    And can access values like

    console.log(a.name);
    
  • 相关阅读:
    LVS三种模式配置及优点缺点比较
    LVS负载均衡-DR模式
    Nginx反向代理和负载均衡
    LNMP(centos7.6 Nginx1.16 MySQL5.7 PHP7 )
    Nginx常用模块
    表演中的晴天
    迷雾中前行
    mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    ubuntu16 安装elasticsearch6.3问题
  • 原文地址:https://www.cnblogs.com/chucklu/p/14184714.html
Copyright © 2011-2022 走看看