Day: September 19, 2017

Create Private Property in Javascript Object

In this lesson, we will see how to create private properties in Javascript object Object with public property I am creating constructor function to create object. I have used statement inside the function. Property name is public here and you can access it from the outside of the function

You will get the […]

Read more

Add Method to Built-in Javascript Object

You can use prototype property to add method to built-in Javascript objects I am going to add inArray() to Array object in Javascript

So number 9 is not array and you will get the output false

Read more

Check Given Property is Own Property of an Object in Javascript

You can check the given property is an own property of an object. You can use the hasOwnProperty() of the object

You can have the following output >name is a property of product true >price is a property of product false

Read more

List All Properties in Object in Javascript

You can use for loop to list all properties and methods in object

Output will be name : Burger category : food price : 10 getDiscount :function () { return this.price * 5 / 100; }

Read more

Prototype Property in Javascript Object

Prototype property of object points to another object in javascript. We will see how we can use the prototype property

Now you can call the getDiscount() to get the discount >console.log(“Discount is “+p.getDiscount()) >Discount is 0.05

Read more

Comparing Two Objects in Javascript

You can compare two objects in Javascript to make sure there refer to the same instance

You can see following output in browser console >Are these equal? false In this example, I am creating two objects with same property but they are not equal.

Read more

Pass Object as an argument in Javascript

I have defined product as below

Now I am going to create an object using above constructor function var product= new Product(“Burger”) Now I want to change that object to represent drink name Coca Cola Lets define method to change its properties

Here you are passing reference to the product object. So whatever […]

Read more

Object Creation in Javascript

There are two ways to create object in Javascript Method 1 Creating objects with object literals In this method you can create properties and method in following way

I have three properties name,category and price. You can see getDiscount() method too With this object you can do following operations To set the property of […]

Read more

Factory Method Design Pattern in Javascript

This is the ShirtFactory constructor used to create LongSleeveShirt and ShortSleeveShirt objects

I am using this client code to create shirt object and call the make() of each object // client program

First I am creating the ShirtFactory object sf and then call the createShirt() by passing argument to receive the […]

Read more