— Simple object method
var rabbit = {};
rabbit.speak = function(line) {
console.log("The rabbit says '"+ line + "'");
};
rabbit.speak("I'm alive.")
has an output of
The rabbit says 'I'm alive.'
— Object method & this
object.method()
— the special variable this
in its body will point to the object that was called onfunction speak(line) {
console.log("The "+ this.type + " rabbit says '" +
line + "'");
};
var whiteRabbit = {type: "white", speak: speak};
whiteRabbit.speak("Oh my ears and whiskers, " +
"how late it's getting!");
has an output of
The white rabbit says 'Oh my ears and whiskers, how late it's getting!'
- apply & call
apply
& call
can be used for object.method()
apply
& call
methods both take a first argument that can be used to simulate method callsthis
function speak(line) {
console.log("The "+ this.type + " rabbit says '" +
line + "'");
};
var whiteRabbit = {type: "white", speak: speak};
speak.apply(whiteRabbit, ["Burp!"]);
speak.call({type: "old"}, "Oh my.");
has an output of
The white rabbit says 'Burp!'
The old rabbit says 'Oh my.
prototype
— prototype of an empty object
Object.prototype
var empty = {};
console.log(empty.toString);
console.log(empty.toString());
has an output of
[Function: toString]
[object Object]
— default property of other objects (array, function …)
Object.prototype
as their prototype, but has its own default propertiesFunction.prototype
& arrays derive from Array.prototype
console.log(Object.getPrototypeOf(isNaN) ==
Function.prototype);
console.log(Object.getPrototypeOf([]) ==
Array.prototype);
has an output of
true
true
— Object.create to create an object with a specific prototype
var protoRabbit = {
speak: function(line) {
console.log("The " + this.type + " rabbit says '" +
line + "'");
}
};
var killerRabbit = Object.create(protoRabbit);
killerRabbit.type = "killer";
killerRabbit.speak("SKREEE!");
has an output of
The killer rabbit says 'SKREEE!'
— constructor prototype
new
keyword in front of it causes it to be treated as a constructorthis
variable bound to a fresh object, and unless it explicitly returns another object value, this new object will be returned from the callnew
is said to be an instance
of its constructorfunction Rabbit(type) {
this.type = type;
}
var killerRabbit = new Rabbit("killer");
var blackRabbit = new Rabbit("black");
console.log(blackRabbit.type);
has an output of
black
— constructor has Object.prototype by default
prototype
, which by default holds a plain, empty object that derives from Object.prototype
function Rabbit(type) {
this.type = type;
}
var blackRabbit = new Rabbit("black");
Rabbit.prototype.speak = function(line) {
console.log("The " + this.type + " rabbit says '" +
line + "'");
};
blackRabbit.speak("Doom...");
has an output of
The black rabbit says 'Doom...'
— same prototype name
console.log(blackRabbit.teeth)
printssmall
because blackRabbit
object does not have teeth
property and it inherits from Rabbit
object’s own teeth
property , which is small
function Rabbit(type) {
this.type = type;
}
var blackRabbit = new Rabbit("black");
var killerRabbit = new Rabbit("killer");
Rabbit.prototype.teeth = "small";
console.log(killerRabbit.teeth);
// small
killerRabbit.teeth = "long, sharp, and bloody";
console.log(killerRabbit.teeth);
// long, sharp, and bloody
console.log(blackRabbit.teeth);
// small
console.log(Rabbit.prototype.teeth);
// small
— enumerable vs. nonenumerable
toString
did not show up in the for/in
loop but the in
operator returned true
for itObject.prototype
are all nonenuerable, which is why they do not show up in such a for/in
loopvar map = {};
function storePhi(event, phi) {
map[event] = phi;
}
storePhi("pizza", 0.069);
storePhi("touched tree", -0.081);
Object.prototype.nonsense = "hi";
for(var name in map) {
console.log(name)
}
console.log("nonsense" in map);
console.log("toString" in map);
has an output of
pizza
touched tree
nonsense
true
true
Object.defineproperty
function, which allows us to control the type of property we are creatingmap.hiddenNonsense
but it won’t show up in a loopvar map = {};
function storePhi(event, phi) {
map[event] = phi;
}
storePhi("pizza", 0.069);
storePhi("touched tree", -0.081);
Object.defineProperty(Object.prototype, "hiddenNonsense",
{enumerable: false, value: "hi"});
for (var name in map) {
console.log(name);
}
console.log(map.hiddenNonsense);
has an output of
pizza
touched tree
hi
— hasOwnProperty vs Object in
hasOwnProperty
methods tells us whether the object itself has the property, without looking at its prototypesin
operator gives usconst map = {};
console.log("toString" in map);
console.log(map.hasOwnProperty("toString"));
has an output of
true
false
for/in
loop like thisfor (var name in map) {
if (map.hasOwnProperty(name)) {
// ... this is an own property
}
}
Object.create
function allows us to create an object with a spefici prototypenull
as the prototype to create a fresh object with no prototypehasOwnProperty
because all the properties the object has are its own propertiesfor/in
loops, no matter what people have been doing to Object.prototype
var map = Object.create(null);
map["pizza"] = 0.069;
console.log("toString" in map);
// false
console.log("pizza" in map);
// true
Thank you for reading!
#javascript #programming #web development