2011-09-04 10 views
1

あなたは次の2つの変数の違いを教えてください。JavaScriptの変数タイプ

var test = {}; 
var test2 = []; 

両方とも配列変数を宣言していませんか?そうでない場合は、どちらを使用するのですか?

答えて

4

最初のオブジェクト(プロパティのない空のオブジェクト)が作成されます。

2番目の列は空の配列を作成します。

のオブジェクトを操作する例を見てみましょう:

var test = { firstName: 'Foo', lastName: 'Bar' }; 
alert(test.firstName); 

あなたはまた、動的に既存の空のオブジェクトを拡張し、それにプロパティを追加することができます。

var test = { }; 
test.firstName = 'Foo'; // or an equivalent: test['firstName'] = 'Foo'; 
alert(test.firstName); 

と配列:

var test2 = [ { firstName: 'Foo', lastName: 'Bar' }, 
       { firstName: 'Foo 2', lastName: 'Bar 2' } ]; 
for (var i = 0; i < test2.length; i++) { 
    alert(test2[i].firstName); 
} 

または要素を配列に追加する:

第二の可変 test2が配列され、固定していながら
var test = { firstName: 'Foo', lastName: 'Bar' }; 
var test2 = [ ]; 
test2.push(test); // the array contains 1 element now 
alert(test2[0].firstName); 
+2

*配列? –

+1

それでは、最初は連想配列で、2番目は通常のインデックス配列ですか? –

+0

この例は、配列とオブジェクトの違いを説明する最善の方法ではないと思います。それらの違いを指摘したい場合は、オブジェクトの配列を作成するのは混乱します; – Harmen

3

最初の変数testは、可変keysvaluesを持つオブジェクトであるkeys(0、1、2、3、...)

例えば:

var test = ['a', 'b', 'c']; 

alert(test[0]);  // alerts 'a' 


var test2 = { 
    first: 'a', 
    second: 'b', 
    third: 'c' 
}; 

alert(test2.first); // alerts 'a' 
alert(test2['first']); // alerts 'a' 
1

最初は、第二は、(オブジェクト自体である)配列オブジェクトであるオブジェクト表記です。

オブジェクトには連想データを格納できますが、配列キーは数値のみにすることができます。

+3

*配列キーは数値のみです。* ...配列もオブジェクトなので、文字列のプロパティも設定できます。あなたは*このように配列を使うべきではありませんが、それはあなたが*できないということを意味しません。 –

0

ここでは、私があなたに例と詳細を説明する準備をするコードのリンクを提供します:JavaScriptの変数の宣言と型は、リンクをクリックしてコードを読んで、テストしてみましょう。

https://code.sololearn.com/Wef3g7HLH5SM/?ref=app

ベローはコードです:*オブジェクトの

<!-- 

In JavaScript you don not have to declare explicitly the type of variables, however JavaScript variables 
can hold many data types: numbers, strings, objects. Below I am going to provide you couple examples, 
I hope they help you! 

--> 

<!DOCTYPE html> 
<html> 
    <body> 

    <p id="demo"></p> 

    <script> 
    // Single line comment 

    /* 
     Multi-lines comment 
    */ 

    /* 
     The typeof operator can return one of these primitive types: 

     * string 
     * number 
     * boolean 
     * null 
     * undefined 
    */ 


    var person = undefined; // a variable without a value, has the value 'undefined' 
    var vehicle = "";  // The value is "", the typeof is "string" 
    /* 
     null type, is "nothing". It is supposed to be something that doesn't exist. 
    */ 
    var house = null;  // Value is null, but type is still an object 
    var number = 1; 
    /* 
     JavaScript has dynamic types. Meaning that 
     you can use the same variable to hold different data types. 
    */ 
    var x;    // Now x is undefined 
    var x = 6;   // Now x is a Number 
    var x = "Sam";  // Now x is a String 

    /* 
     Numbers can be written with, or without decimals places 
    */ 
    var x1 = 2.50;  // Written with decimals 
    var x2 = 5;  // Written without decimals 

    /* 
     Extra large or extra small numbers can be written with 
     scientific (exponential) notation 
    */ 
    var a = 123e4;  // will print: 1230000 
    var b = 123e-4;  // will print: 0.

    /* 
     Booleans can only have two values: true or false. 
    */ 
    var y = true; 
    var z = false; 

    /* 
     Single or double quotes can be use to write Strings. 
    */ 
    var stringVariable1 = "This is my name: 'Sam'"; // Using single quotes 
    var stringVariable2 = 'Here, my name: "Sam"'; // Using double quotes 

    /* 
     JavaScript arrays are written with square brackets. 
     Array items are separated by commas. 
    */ 
    var girls = ["Julia", "Mary", "Olivia"]; 

    /* 
     JavaScript objects are written with curly braces. 
     Object properties are written as name: value pairs, separated by commas. 
    */ 
    var userData = {firstName:"John", lastName:"Doe", userName:"JDoe", 
     password:123456}; 

    document.getElementById("demo").innerHTML = 


    typeof person + "<br>" + 
    typeof vehicle + "<br>" + 
    typeof number + "<br>" + 
    typeof y + "<br>" + 
    typeof house + "<br>" + 

    number + "<br>" + 
    x + "<br>" + 
    x1 + "<br>" + 
    x2 + "<br>" + 
    a + "<br>" + 
    b + "<br>" + 
    y + "<br>" + 
    z + "<br>" + 
    stringVariable1 + "<br>" + 
    stringVariable2 + "<br>" + 
    // here I am going to print the value on array, index 0 
    girls[0] + "<br>" + 
    userData.firstName + "'s userID: " + userData.userName + 
         " and pwd: " + userData.password; 
    </script> 

    </body> 
</html>