Section 4: Objects and Functions Flashcards
What values can hold an Object ? and how they are called?
A primitive that is called a property
An object that is called a property also
Functions that are called methods
An object have reference in memory to their peorperties and methods?
YES
How is important to think about an Object as…
(When we talk about how it´s an object in memory)
As sitting in memory, and then having references to other things sitting in memory that are connected to it, so it knows where its different properties and methods are, that is: primitives, objects and functions. (properties and methods)
How can we access those slots in memory, those properties and methods of an object?
and how are their names(of the operators)?
With 2 operators:
the dot operator . also called member access
the computed memeber access operator []
What does the computed member access operator [] ?
and help me to remember what an operator is, under the hood?
Remember, an operator is a function.
The computed member operator, takes two parameters and it’s associativity is left-to-right
to the left it takes the object, and inside the [] it takes the property or method
What does the dot . operator do?
It’s a fnction and when used after an object it takes two parameters, the object that you’re looking at and the name of the property.
It takes the object(on the left) and on the right the string that represents the name of the value that you are looking for
What is the other name of the dot . operator?
It´s also called Member Access
Why the dot . operator is called Member Access
Because members being members of the object, These are looking for members of the object, the pieces, the methods, and the properties
How can we create an object with object literals
using curly braces { }
What is inside the curly braces { } how is it treated, how many lines of code?
It´s treated as one line of code
When it gets really powerful to use object literals?
Because we can create objects on the fly
For example, on passing parameters to a function, as an object literal
Let´s pass some object as a parameter to this function, how can we imagine this.
function greet(person) {
console.log(“Hi ” + person.firstname);
}
We can call the greet funciton, and create an object on the fly
greet({
firstname: ‘Tony’,
lastname: ‘Garcia’
});
What “think” the JS engine when it sees:
greet({
firstname: ‘Tony’,
lastname: ‘Garcia’
});
it says:
“oh, you are calling a function, and oh, what are you gonna pass to it? Well, I just saw curly braces { } so I know you´re creating an object right here at this moment of execution and passing it, it`d be no different than passing anything else like a number or a string”
It´s true to say that “Wherever i want I can create an object as I go, and I can use an object wherever I use any variable”?
YES
What is a Namespace?
A container for variables and functions, typically to keep variables and functions with the same name separate.
JS have namespaces? and why?
NO, because of the nature of how objects works inJS, we don´t need it as a feature
How can we fake namespaces, for preventing collisions like this one:
var greet = “hello!”;
var greet = “hola!;
console.log(greet) // hola!
by creating an object that will be the container for our properties and methods and the things we want to use. The object it won’t really have any other functionality, and that’s okay
var english = {
greetings: {
basic: ‘Hello’
}
}
console.log(english.greetings.basic);
What is the idea of using objects as faking namespaces? and why?
We need to containe variables, methods, functions, properties from the global namespaces.
Because someone else might have written or even that we´ve written code in the global namespace and forgotten about.
What it means JSON, and what is JSON
Javascript object notation.
It´s just a string of data
In a JSON format, how properties needs to be wrapped?
in quotes “ “
JSON is a part of JS?
No
Imagine a JSON format file, with two properties, how it will look?
{
“firstname”: “Mary”,
“isAProgrammer”: true
}
How can we set a valid object literal in JS to a JSON format?
using the JSON.stringify() method
How can we pass a valid JSON string of data, to an Object in JS?
using JSON.parse() method
1) Anything that is JSON valid is also valid JS object literal syntax?
2) All object literal syntax is valid JSON?
1) YES
2) NO
What is the meaning of “first class functions”
Everything you can do with other types you can do with functions. Assign them to variables, pass them around, create them on the fly
In JS, functions are objects?
YES
Does a function object resides in memory?
Yes, it resides in memory
Does a function object, has all the features of a normal object and has some other special properties?
YEs
Can you attach properties and methods to a function? and why?
Yes, you can.
Because it´s just an object
Does function objects has some hidden special properties?
YES
What are the hidden properties of a function object?
the code property
and the name property (it could also be anonymous)