visit
A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package.
let Person = {
name: "Foziya",
age: 20,
action: ["walk", " run"],
greeting: function() {
console.log("Hello");
}
};
let shape = {
name: "rectangle",
color: "red",
size: {
length: 10,
breadth: 20
}
};
console.log(shape);
// { name: 'rectangle',
// color: 'red',
// size: { length: 10, breadth: 20 } }
console.log(shape.size.length)
// 10
let one = 1;
let two = 2;
let three = 3;
let numbers = {
one: one,
two: two,
three: three
};
console.log(numbers);
//{ one: 1, two: 2, three: 3 }
let one = 1;
let two = 2;
let three = 3;
let numbers = { one, two, three };
console.log(numbers);
//{ one: 1, two: 2, three: 3 }
console.log(numbers.one)
// 1
console.log(numbers.one === { one }.one);
// true
let Person = {
name: "Ney Vatsa",
name: "Shashank"
};
console.log(Person.name);
// Shashank
New
KeywordThe
Object
constructor creates an object wrapper for a given value. If the value is or , it will create and return an empty object. Otherwise, it will return an object of a type that corresponds to the given value.Objects can also be created using the
new
keyword. With the built-in Object Constructor in Javascript, new
creates an empty object; or, this keyword can be used with a user-defined constructor function: with builtin Object Constructor
.To get started, first take a look at this example:
let movies = new Object();
console.log(movies)
//{}
The next step is to add properties and methods to this empty object. This can be achieved with simple dot notation:
let movies = new Object();
console.log(movies)
//{}
movies.name = "Dead Poets Society";
movies.releaseYear = 1989;
movies.genre = ["Drama", "Teen"];
movies.ratings = {
IMDb: "8.1 / 10",
Metacritic: "79%"
};
movies.watch = () => {
console.log("Watch Online");
};
console.log(movies);
// { name: 'Dead Poets Society',
// releaseYear: 1989,
// genre: [ 'Drama', 'Teen' ],
// ratings: { IMDb: '8.1 / 10', Metacritic: '79%' },
// watch: [Function] }
movies.watch();
// Watch Online
function movies(name, releaseYear, genre, ratings) {
this.name = name;
this.releaseYear = releaseYear;
this.genre = genre;
this.ratings = ratings;
this.watch = () => {
console.log("Watch Online");
};
}
let DPS = new movies("Dead Poets Society", 1989, ["Drama", "Teen"], {
IMDb: "8.1 / 10",
Metacritic: "79%"
});
console.log(DPS);movies {
// name: 'Dead Poets Society',
// releaseYear: 1989,
// genre: ['Drama', 'Teen'],
// ratings: { IMDb: '8.1 / 10', Metacritic: '79%' },
// watch: [Function]
// }
let rocky = new movies("Rocky", 1976, ["Drama", "Sports"], {
IMDb: "8.1 / 10",
Metacritic: "70%"
});
console.log(rocky);
// movies {
// name: 'Rocky',
// releaseYear: 1976,
// genre: ['Drama', 'Sports'],
// ratings: { IMDb: '8.1 / 10', Metacritic: '70%' },
// watch: [Function]
// }
class Movies {
constructor(name, releaseYear, genre, ratings) {
this.name = name;
this.releaseYear = releaseYear;
this.genre = genre;
this.ratings = ratings;
}
watch() {
console.log("Watch Online");
}
}
let rocky = new Movies("Rocky", 1976, ["Drama", "Sports"], {
IMDb: "8.1 / 10",
Metacritic: "70%"
});
console.log(rocky);
// Movies {
// name: 'Rocky',
// releaseYear: 1976,
// genre: ['Drama', 'Sports'],
// ratings: { IMDb: '8.1 / 10', Metacritic: '70%' }
// }
rocky.watch();
//Watch Online
/*
above example
*/
rocky.buy = function() {
console.log("Buy the Movie");
};
rocky.buy();
// Buy the Movie
Being familiar with classes is extremely helpful, as popular JavaScript libraries such as make frequent use of the
class
syntax.Previously published at