In JavaScript, why can non-array objects also use Obj ["Index"] to access properties?
according to MDN"s property accessor ,
objects can access properties using obj.propertyName / obj [" propertyName "].
and the properties of the array can be accessed through the subscript. (the source forgot, but I just know)
but I found that for non-array objects, you can also use the subscript to access the properties of the object (although I don"t know what the subscript is, it"s scribbled). Why?
var a = [],i = 0;
var obj={
a:12,
b:34.56,
c:true,
d:"hello",
e:null,
f:undefined,
g:{}
};
for (a[i] in obj) {
console.log("a[" + i + "]=" + a[i] + ",obj[" + i + "]=" + obj[a[i]]);
iPP;
}
:
a [0] = aPowerobj [0] = 12
a [1] = bpjobj [1] = 34.56
a [2] = c br obj [2] = true
a [3] = djue obj [3] = hello
a [4] = ejynobj [4] = null
a [5] = fjinger obj [5] = undefined
a [6] = Gregorian obj [6] = [
there are two ways to access the properties of an object:
- Obj.attr
- Obj [attr]
sometimes the first kind cannot be implemented. Let me cite a chestnut:
therefore, a second method is provided to get the value of the attribute.
access attributes using the attribute expression
.
objects can access properties using obj.propertyName / obj ["propertyName"].
and the properties of the array can be accessed through the subscript
for non-array objects, you can also use the subscript to access the properties of the object
these three sentences are all true, in fact, the core is the first sentence.
objects can access properties using obj.propertyName / obj ["propertyName"].
because the array is an object,
arrays can access properties using obj.propertyName / obj ["propertyName"].
but because propertyName
needs to be a string, arr.1
will report an error. In this case, use the second, obj ["propertyName"]
, here is arr ['1']
, and of course arr [1]
is also possible, because the content in []
will be automatically converted to character type if it is not a character type, and will be evaluated if it is a variable.
for basic knowledge, see ide-6th Edition-medium-scan Edition- fill it up. The knowledge on the Internet is uneven, so there is no need to read it.
finally found the source of this knowledge point. See ide/Working_with_Objects" rel=" nofollow noreferrer "> here
object attribute index
in JavaScript 1. 0, you can access a property by name or ordinal. But in JavaScript 1.1 and later, if you initially define an attribute with a name, you must access it by name, and if you initially use an ordinal to define an attribute, you must access it by index.
this limitation occurs when you create an object and its properties through the constructor (as we did earlier with the Car object type) and explicitly define individual properties (such as myCar.color = "red"). If you initially use an index to define an object property, such as myCar [5] = "25", you can only reference it through myCar [5].
The exception to this rule is
from objects that correspond to HTML, such as the forms array. For the elements of these arrays, you can always access them either by their ordinal number (according to the order in which they appear in the document) or by their name (if any). For example, if the second < form > tag in the document has a NAME attribute and the value is "myForm", the form can be accessed by document.forms [1], document.forms ["myForm"], or document.myForm.
var myCar=new Object();
myCar.make="Ford";
myCar.model= "Mustang";
myCar.year = 1969;
myCar["hum"] = 100;
console.log(myCar["0"]);
console.log(myCar["1"]);
console.log(myCar["2"]);
console.log(myCar["hum"]);
console.log(myCar.hum);