Sie sind auf Seite 1von 2

Objects and functions, in Javascript, theya re very very much related.

They are
inmany ways the same subject.

Objects and the Dot

Objects are a collection of name value pairs. and those values can be other
collections of name-value pairs.

How an object lives in the memory.

Objects have properties and methods.

a primitive [type] sitting off of it - that is called a property. Like a boolean,


or a number or string.

an object can have another object connected to it - that is another property. This
is called a child.

An object can also contain functions, these are called methods. It is a function
but is connected to an object so it is called a method.

Objects have properties and methods and they sit in memory:


a. the core object will have an address
b. the core object will have references [to the addreses] to this porperties and
methods. they may be or not related to the address of the core object.

So an object is something that sits in memory which has referneces to other things
connected to it in the memory.

HO wdo we access those slots in memory.

code:
var person = new Object(); //creating a new object

person["firstname"] = "Tony"; /*computed member access operator: []. create a spot


in memory and give it the name "firstname". The person object will get a refernece
to the address of that location in the memory. This is a primitive, a string. so
it is a property - firstname property.

This is one way to set an object property. See Operator precednce top 3. The []
operator takes the object name and the property name as parameters.*/

person["lastname"] = "Alicea";

var firsnameProperty = "firstname";

console.log(person);
console.log(person[firstNameProperty]);

output:
{firstname: "Tony", lastname: "Alicea"}firstname: "Tony"
lastname: "Alicea"
__proto__: Object
Tony

code:
var person = new Object(); //creating a new object
person["firstname"] = "Tony";

person["lastname"] = "Alicea";

var firsnameProperty = "firstname";

console.log(person);
console.log(person[firstNameProperty]);

console.log(person.firstname); //top 2 in the precedence. takes object name, and


name of property. Member access operator: "."

person.address = new Object();


person.address.street = "111 Main St.";
person.address.city = "New York";
person.address.state = "NY"; //see associativity of this operator.
console.log(person.address.street);
console.log(person.address.city);
console.log(person.address.state);
output:
{firstname: "Tony", lastname: "Alicea"}firstname: "Tony"
lastname: "Alicea"
__proto__: Object
Tony
Tony

.: person["firstname"] == person.firstname
.: person["address"]["state"] == person.address.state

these dots and brackets are just functions. operators. obejcts are just things in
memory woth reference to to other things in memory.

the preffered approach is the dot. more readable. unless you want to access the
object via string

Das könnte Ihnen auch gefallen