JSFiddle - React, Tailwind, and code Playground

by dshilkret

JavaScript

function Person(name) {
    this.name = name;
}

Person.prototype = {
    color: "blue", 
    greet: function() {
        return 'Hello, ' + this.name;
    }
};

var alice = new Person('Alice');

for (var key in alice) {
    if (alice.hasOwnProperty(key)) {
        console.log(key + ': ' + alice[key]);
    } else {
        console.log('Inherited property: ' + key);
    }
}


// Invoke the greet method
var greeting = alice.greet();

console.log(greeting); // Output: "Hello, Alice"

function Person(name) {
    this.name = name; // 'name' is an "own property" of each instance
}

Person.prototype.color = "blue"; // 'color' is a shared property on Person.prototype
Person.prototype.greet = function() {
    return 'Hello, ' + this.name;
};

var alice = new Person('Alice');

console.log(alice.name);  // Output: "Alice" (own property of alice)
console.log(alice.color); // Output: "blue" (inherited from Person.prototype)
console.log(alice.greet()); // Output: "Hello, Alice" (inherited method from Person.prototype)