JavaScript Object Constructors
// using object literal
let person = {
name: 'Sam'
}
// using constructor function
function Person () {
this.name = 'Sam'
}
let person1 = new Person();
let person2 = new Person();
// Constructor function for Person objects
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
// Create a Person object
const person1 = new Person("John", "Doe", 50, "blue");
console.log(person1);
const person2= new Person('A','B',20,'black');
console.log(person2);
// ouput
Person { firstName: 'John', lastName: 'Doe', age: 50, eyeColor: 'blue' }
Person { firstName: 'A', lastName: 'B', age: 20, eyeColor: 'black' }
example
function User(name) {
this.name = name;
this.isAdmin = false;
return;
}
var user = User("Julie");
console.log(user); // undefined
var obj = {};
function A() { return obj; }
function B() { return obj; }
console.log( new A() == new B() ); // true
Comments
Post a Comment