Objects+Cheat+Sheet
Objects+Cheat+Sheet
JavaScript allows you to write object-orientated code and objects are indeed a
key element of the JavaScript language.
Creating Objects
There are different ways to create an object. Probably the easiest one is this
one:
var myObj = {
value1: ‘a value’,
fn: function() {…}
}
But you may also create objects using this syntax:
var country = new Object();
country.name = 'Italy';
Or this one:
var house = Object.create(null);
house.size = 45;
There even is another one, you’ll see it in the “Constructor Functions” section.
Object.create() offers the advantage, that you can pass the prototype of the
to-be-created-object as an argument (or null, if no prototype should be set).
You’ll learn more about Prototypes in this document, but that is a powerful
feature which allows you to overwrite JavaScript’s default behavior.
Prototypes
Prototypes are objects on which other JavaScript objects are based. It’s
JavaScript’s form of inheritance between objects.
Prototypes are important because they provide functionality to an object,
which the object itself might not have.
For example, a prototype may have a greet() method. An object based on that
prototype might not have that, but you could still execute obj.greet() since
JavaScript will refer to the Prototype (and then the Prototype’s Prototype and
so on) to find properties or methods accessed by the code.
By default, objects created via literal notation or new Object() inherit from the
Object.prototype. You can think of this as JavaScript’s default prototype. It
ships some useful methods like toString() to convert the object to a string
which can be printed to the console for example.
Constructor Functions
Constructor functions allow you to create your own objects and prototypes. It’s
best to simply see it in action:
var Person = function() {
this.name = '';
this.greet = function() {
console.log('Hi, my name is ' + this.name +
' and I am ' + this.age + ' years old!');
}
};
var max = new Person();
As you see, you simply create a normal function and then you may use this as a
constructor for your own objects. Might look strange, but that’s how it works
in JavaScript.
You set properties for your object by using the this keyword in the function
(e.g. this.name).
Objects based on that constructor (created, using new) have all the properties
and methods set up in the function.
You can add properties and methods to this prototype as well. This allows you
to extend your base object without touching the constructor function.
This
The this keyword is very important in JavaScript. As you saw in the constructor
section, it allows you to add properties to an object. And in general, it there
referred to the object itself.
In line with the above statement, in most cases this simply refers to the
following: The object executing the code in which this is called.
You can control the value of this with the bind(), call() and apply() methods.
These methods allow you to overwrite the default this value.
Read this great answer to understand how they work and differ:
http://stackoverflow.com/a/31922712
defineProperty()
Learn more about available configurations here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Obje
cts/Object/defineProperty
If you want to loop through all fields/ properties an object has, you can use the
for … of loop:
for (field in rect1) {
console.log(field + ': ' + rect1[field]);
}