oop in javascript code example

Example 1: js oop

function Person(name) {
  this.name = name;
  this.greeting = function() {
    alert('Hi! I\'m ' + this.name + '.');
  };
}

Example 2: oops in javascript

const book = {   title: "Hippie",       author: "Paulo Coelho",     year: "2018"}

Example 3: js oop

function createNewPerson(name) {
  var obj = {};
  obj.name = name;
  obj.greeting = function() {
    alert('Hi! I\'m ' + this.name + '.');
  };
  return obj;
}

Example 4: js oop

var salva = createNewPerson('Salva');
salva.name;
salva.greeting();

Example 5: js oop

let names = {
    fname: "Dillion",
    lname: "Megida"
}
console.log(names.fname);
console.log(names.hasOwnProperty("mname"));
// Expected Output
// Dillion
// false

Example 6: is javascript object oriented

var js = new JavaScript();
js.isObjectOrientated = true;

Tags:

Java Example