js oop tutorial code example

Example 1: js oop

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

Example 2: js oop

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

Example 3: js oop

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

Example 4: is javascript object oriented

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

Example 5: oop javascript

function DogObject(name, age) {
    let dog = Object.create(constructorObject);
    dog.name = name;
    dog.age = age;
    return dog;
}
let constructorObject = {
    speak: function(){
        return "I am a dog"
    }
}
let bingo = DogObject("Bingo", 54);
console.log(bingo);

Tags:

Java Example