javascript create object with method code example

Example 1: creating an object javascript

let obj = {
// fields
  name:"value",
  num: 123,
//methods
  foo: function(){}
  
}

Example 2: js create object with properties

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Example 3: make an object javascript

class ObjectLayout {
	constructor() {
    	this.firstName = "Larry"; //property
    }
  
  	sayHi() { // Method
    	return `Hello from ${this.firstName}`;
    }
}

const object = new ObjectLayout();
// object.firstName is Larry;
// object.sayHi() gives "Hello from Larry"

Example 4: how to make new paruser object

ParseUser parseuser = new ParseUser(); // It's just a simple class object

Tags:

Misc Example