JavaScript data types include simple data types such as numbers, strings and booleans, and complex data types such as objects.
Objects
An object is an unordered list of data stored in name-value pairs. Property values in an object may be of any data type, including arrays, functions and strings. An example of an object would be the following:
var newObject = { name: "Koren", city: "New York" }
Accessing and Adding Name-Value Pairs
You can access object values in the following ways:
newObject.name; newObject["name"];
Adding name-value pairs to an object can be accomplished as follows:
newObject.last_name = "Cohen"; newObject["last_name"] = "Cohen";
Object Literal Notation vs. Constructors
Object literal notation and constructors describe the manners in which an object can be created. In the above example, the object newObject was created through object literal notation, meaning an object was created with a list of name-value pairs wrapped in curly braces.
A constructor is a function used to initialize new objects using the new keyword. All objects that inherit from another object also inherit a constructor property. An object is created with a constructor as follows:
var newObject = new Object();
Custom Constructors
JavaScript also allows for the use of custom constructors. Take the following function, for example:
function Person (name, city) { this.name = name; this.city = city; }
You would then be able to use Person as a custom constructor, instead of merely calling a function:
var koren = new Person("Koren", "New York");
Prototypes
Prototypes are important in JavaScript because JavaScript does not have classical inheritance based on classes. Prototypes also allow for reuse of common objects.
Prototypes differ from traditional object-oriented classes as they are simpler and more concrete in the sense that they are examples of objects rather than descriptions of the manner in which to initialize an instance of a class.
Using the Person function above, a prototype may look something like:
Person.prototype.hello = function() { console.log("Hello!"); }
This prototype inherits from Person and creates methods that can be used on all instances of the Person object, as follows:
var koren = new Person("Koren", "New York"); koren.hello();
Using Prototypes to Call Functions on Common Data Types
Prototypes can also be used to create functions which may be called on common data types, such as strings or arrays. For example, if you wanted to call a function spaces on a string, you could create a prototype as follows:
String.prototype.spaces = function() { alert(this.split('').join(' ')); }
Now, you can call the spaces function directly on a string as follows:
var str = "Koren"; str.spaces();
This will result in the following alert: K o r e n.
Similarly, if you wanted to call a duplicate function on an array, you could use the following prototype:
Array.prototype.duplicate = function() { alert(this.concat(this)); }
You may now call the duplicate function directly on an array:
var arr = [1, 2, 3, 4, 5]; arr.duplicate();
This will result in the following alert: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.
- PM Career Story - April 28, 2022
- How to Transition into Product Management - December 26, 2017
- What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015
Comments