9/17/2016 - 8:08 PM

Javascript objects notes

Javascript objects notes

Object size

There's no native size function.

Cookies saved shouldn't exceed 4093 bytes, thus we need to ensure we don't have an object of bigger size.

This stackoverflow answer checks the estimated size of the object

Different Syntax for objects creation

First Way

AKA: Using object constructor.
By letting values/keys get decided later in the code

var data = {};
data.first_name = 'Ahmed';
data.last_name = 'Adel';
data.email = 'ahmed.adel@domain.c';

Second Way

AKA: Using object literals.
By setting up values/keys up front while declaring the object

var data = {
  data.first_name = 'Ahmed',
  data.last_name = 'Adel',
  data.email = 'ahmed.adel@domain.c'

Note: we may extend that for a more OO way so that we could template objects.
In example, by using data as a function and calling new on it for every object.

function datum(first_name, last_name, email) 
  this.first_name = first_name,
  this.last_name = last_name,
  this.email = email
data =new datum(first_name, last_name, email);

Third Way

This way is similar to the first one, but a bit more dynamic

var data = {};
data['first_name'] = 'Ahmed';
data['last_name'] = 'Adel';
data['email'] = 'ahmed.adel@domain.c';

In example: if we have dynamic property name we may do

function datum(first_property, second_property, third_property, val1, val2, val3)
  var data = {};
  data[first_property] = val1;
  data[second_property] = val2;
  data[third_property] = val3;
  return data;
data = datum(first_name, last_name, email, 'Ahmed', 'Adel', 'ahmed.adel@domain.c');


Stackoverflow question.
At this moment of video, we can find the technical namings.
The OOP part of the video goes through this tutorial.