Javascript class object using function

Method 1)

/**
* @class ObjItem
* @type {Object}
* @property {id}
* @property {title}
* @property {startDay}
* @property {category}
*/
function ObjItem(id, title, startDay, category){
this.id = id;
this.title = title;
this.startDay = startDay;
this.category = category;
}


var myItem = new ObjItem(1, "Test Title", "2017-10-04", "Holiday");

Method 2)

/**
* @class ObjItem
* @type {Object}
* @property {id}
* @property {title}
* @property {startDay}
* @property {category}
*/
function makeObjItem(id, title, startDay, category){
var obj = {
id: id,
title: title,
startDay: startDay,
category: category
};
return obj;
}


var myItem = makeObjItem(1, "Test Title", "2017-10-04", "Holiday");

Reference from :
https://stackoverflow.com/questions/12272239/javascript-function-returning-an-object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: