JSON / Objects / Prototypes
all objects in JavaScript....
- are descended from Object - inherit methods and properties from Object.prototype
Arrays derive from....
Array.prototype
Functions derive from....
Function.prototype
what is JSON?
JSON is a data storage and communication format based off of JavaScript object literals - all property names are surrounded by double quotes - values are restricted to simple data: no function calls, variables, comments, or computations
what does JSON stand for?
JavaScript Object Notation
how do you retrieve an object's original prototype?
Object.getPrototypeOf(obj)
what is the default value of a constructor's prototype?
a plain, empty object that derives from Object.prototype
how do you remove a property?
delete obj.prop
using this creates a property name and value for....
every new object instantiated with the constructor - each object has its own copy - this properties are considered "own" properties (they havent been inherited
what does stringify(value) do?
returns a JSON string representation of the value passed in
what does parse(text) do?
returns an object created from the supplied JSON text
what does hasOwnProperty method do?
returns true if property was set on actual object rather than inherited
can you assign an entire object to the prototype property? if yes how
yes function Cat(){this.cute = 'very'} function Kitten(){} Kitten.prototype = new Cat(); var k = new Kitten(); console.log(k.cute);
function invocation, this refers to ....
the global object
what does a standalone function's this refer to?
the global object
in a method invocation this refers to
the object the method was called on
if you set properties on the prototype objects....
these properties are not own properties c.hasOwnProperty(c.cute) --> false bc cute is inherited
apply, call, and bind....
this is whatever you pass in as the first argument
when a function is called as a method, as in obj.method(), what will this point to?
this will point to the object that it was called on
what are some properties that are inherited from Object.prototype?
toString hasPropertyOf
what will obj.prototype return
undefined
show how Object.create works
var cat = {cute: 'very'}; var kitten = Object.create(cat); console.log(kittens.cute) //kitten inherits cute from cat
what is a constructor?
a function with the new keyword in front of it - make the first letter of a constructor uppercase - this helps distinguish between regular functions and constructors
a prototype is ....
another object that is used as a fallback source of properties
the constructor's this object is...
bound to a fresh, empty object
how else can you create an object with a particular prototype?
constructors
what does Object.create do?
creates a new object with the specified prototype object and properties
how can you add properties to an object even after its instantiated?
function Cat(){}; var c = new Cat(); Cat.prototype.cute = 'very'; console.log(c.cute)
how do you add a new property and value pair to an object?
obj.newProp = newVal obj[newProp] = newVal
how can you get/retrieve a value that's associated with a property name from an object?
obj.propName obj[propName]
what are three ways to create objects?
object literals Object.create constructors
all constructors have a property named....
prototype
in addition to their set of properties, almost all objects also have a ....
prototype
the search for a property goes on up the chain of prototypes until....
- the property is found - an object with a null prototype is reached/the last object in the chain Object.prototype
function Werewolf(mood){ this.mood = mood; } what can you think this constructor is doing when invoked with new?
function Werewolf(mood){ //this = {} this.mood = mood; //return this; }
what is Object.prototype's prototype?
null
what is the prototype of an object created with an object literal? Object.getPrototypeOf({})
Object.prototype
what is the top level prototype?
Object.prototype
what is Array.prototype's prototype?
Object.prototype - its at the top of the prototype chain - its the last object checked for properties
when an object gets a request for a property it does not have....
its prototype will be searched for the property, then the prototype's property, and so on
what are methods?
methods are functions within the context of an object