I am trying to understand objects and the dot notation property accessor:
is document.createElement('pre')
the same as saying createElement()
is a property of document
because I am using the dot notation property acccessor? Would that mean properties are also objects?
In the example above, is the dot operator performing a different function than accessing a property, just like the +
operator can perform both addition and concatenation? It looks like createElement
is a property of document
.
I am unable to answer this after reading the following sources:
MDN Property Accessors
Dot Notation vs Bracket Notation
is document.createElement('pre') the same as saying createElement() is a property of document because I am using the dot notation property acccessor?
Yes.
Would that mean properties are also objects?
They can be. Properties have a value, which can be a primitive (number, string, undefined, etc.) or a reference to an object (Array, Object, DOM Element, Function). Where a property references a Function or callable object, it's called a method of the object that has the property.
So in the above, createElement is a method of the document object.
In the example above, is the dot operator performing a different function than accessing a property, just like the + operator can perform both addition and concatenation?
No. Dot property accessors are a convenience and can only be used where the property name is a valid identifier, e.g. you must use square bracket notation for a property with a name of foo-bar since a name with a hyphen is not a valid identifier, but can be used as a property name:
var obj = {'foo-bar': 'fum'};
Square bracket notation also allows the use of variables for property names:
var propName = 'foo-bar';
var obj = {};
obj[propName] = 'fum';
console.log(obj['foo-bar']); // 'fum'
It looks like createElement is a property of document.
It is, so that's a good thing. :-)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments