var arrayList = [1 , 2, 3];console.log( Object.prototype.toString.call(arrayList) === '[object Array]' ); // trueconsole.log( Array.isArray(arrayList) ); // also true...
typeof
is an operator that returns a string with the type of whatever you pass.Symbol
.typeof(null)
will return object.instanceof
is much more intelligent: it works on the level of prototypes. In particular, it tests to see if the right operand appears anywhere in the prototype chain of the left. instanceof doesn’t work with primitive types. It instanceof operator checks the current object and returns true if the object is of the specified type, for example: function helloWorld(name) { return "hello world, " + name;}helloWorld("JS Geeks"); // "hello world JS Geeks"Methods in JavaScript are nothing more than object properties that are functions:
var obj = { helloWorld : function() { return "hello world, " + this.name; }, name: 'John Carter'}obj.helloWorld(); // // "hello world John Carter"We can copy a reference to the same function helloWorld in another object and get a difference answer:
var obj2 = { helloWorld : obj.helloWorld, name: 'John Doe'}obj2.helloWorld(); // "hello world John Doe"A constructor call
new Employee('John Doe', 28)
creates a brand new object and passes it as the value of this
, and implicitly returns the new object as its result. The primary role of the constructor function is to initialize the object:function Employee(name, age) { this.name = name; this.age = age;}var emp1 = new Employee('John Doe', 28);emp1.name; // "John Doe"emp1.age; // 28
function Employee(fName, lName, age, salary){ this.firstName = fName; this.lastName = lName; this.age = age; this.salary = salary; } // Creating multiple object which have similar property but diff value assigned to object property. var employee1 = new Employee('John', 'Moto', 24, '5000$'); var employee1 = new Employee('Ryan', 'Jor', 26, '3000$'); var employee1 = new Employee('Andre', 'Salt', 26, '4000$');
Object Literal is best way to create an object and this is used frequently.
var employee = {name : 'Nishant',salary : 245678,getName : function(){return this.name;}}
var employee = {name : 'Nishant',salary : 245678,address : {addressLine1 : 'BITS Pilani',addressLine2 : 'Vidya Vihar'.phoneNumber: { workPhone: 7098889765, homePhone: 1234567898}}}
new
keywordObject.create(obj)
will create a new object and set the obj as its prototype. It’s a modern way to create objects that inherit properties from other objects. Object.create function doesn’t run the constructor. You can use Object.create(null) when you don’t want your object to inherit the properties of Object.function deepClone(object){var newObject = {};for(var key in object){if(typeof object[key] === 'object' && object[key] !== null ){ newObject[key] = deepClone(object[key]);}else{ newObject[key] = object[key];}}return newObject;}var newObject = deepClone(obj);Explanation: Let's understand in this way you have been given an object
personalDetail
this object contains some property which again a type of object here as you can see address is an object and phoneNumber in side an address is also an object. In simple term personalDetail is nested object(object inside object). So Here deep copy means we have to copy all the property of personalDetail object including nested object. So when we do deep clone then we should copy every property (including the nested object):var personalDetail = {name : 'Nishant',address : { location: 'xyz', zip : '123456', phoneNumber : { homePhone: 8797912345, workPhone : 1234509876 }}}
(function() { console.log("Hi, I'm IIFE!");})();// outputs "Hi, I'm IIFE!"