xxxxxxxxxx
// (String, Number, Array, Date chính là Object Constructor)
function Student(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Object prototype
Student.prototype.getFullName = function() {
return `${this.firstName} ${this.lastName}`
}
// Đổi tượng được tạo ra
const student = new Student('Long', 'Bui')
// Đối tượng Date
const date = new Date();
const day = date.getDate()
const month = date.getMonth() + 1;
const year = date.getFullYear()
Math.PI // Trả về số PI
Math.round() // Làm tròn số
Math.abs() // Dùng để biến số Âm thành số dương
Math.ceil() // Làm tròn trên
Math.floor() // Làm tròn dưới
Math.random() // Trả về ngẫu nhiên số thập phân nhỏ hơn 1
Math.min(1,2,3,4) // Trả về số nhỏ nhất
Math.max(1,2,3,4) // Trả về số cao nhất
xxxxxxxxxx
var person = {
first_name : "Marty",
last_name : "Mcfly",
born : 1968,
died : 1933,
lovers: ["Jennifer Parker","Baines McFly"]
};
xxxxxxxxxx
let object = {
name : "Petya",
age : 15
};
// another way
let object = new Object();
object["name"] = "Petya";
object["age"] = 15;
// old way
var object = new Object()
object.name = "Petya";
object.age = 15;
xxxxxxxxxx
/*
An object is made of key value pairs. Keys can be strings
(which don't require quotes), array with a string, or symbols. Values can be arrays,
objects, numbers etc
*/
let testSymbol = Symbol('item number 3')
const obj = {
item1: 1,
"item number 2": 2,
testSymbol: 3,
['a']: 4
}
// Another way of creating an object:
const obj = new Object();
obj.name = 'John'
// To access values, you can use dot notation or bracket notation.
// Both do the same thing, bracket notion is useful for multispace keys,
// keys with dashes, or accessing values using variables
> obj.item1
> obj['item number 2']
> let b = 'item1'
obj[b]
// The following would NOT work and would return undefined:
obj.b
// Checking exsistence of keys in object:
obj.toString ----- checks values of object and whatever object inherits from
> returns true
obj.hasOwnProperty('toString') ----- checks values of object only. Do this instead of checking like: (obj.name !== undefined)
> returns false
// Short hand key assignment:
const name = 'John'
const obj2 = {
// this is short hand that automatically sets the key to be 'name',
// and it's value to be 'John'
name,
}
// Constructor objects:
function Obj(name, age){
this.name = name
this.age = age
}
const person = new Obj('john', 1)
// adding functions, couple ways:
const obj = {
name: 'Mike',
number: 4421,
sayHi: () => console.log('hi'),
sayBye() {
console.log('say bye')
},
// getter function to get values from object. Has different usecases, but the same as doing obj.number
get getNumber(){
return this.number
}
// setter function to set values in object. Has different usecases, but the same as doing obj.number = 152
set setNumber(num){
this.number = num
}
}
obj.sayHi()
obj.sayBye()
obj.getNumber //Note how it's being accessed like a standard property, and not a function
obj.setNumber //Note how it's being accessed like a standard property, and not a function
object in js
xxxxxxxxxx
const person = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
console.log(person); // logs {firstName: "John", lastName: "Doe", age: 30}
The object data type is used to represent a collection of key-value pairs. An object can be created using the curly braces {} syntax or the new Object() syntax. For example:
xxxxxxxxxx
const obj = {
name: "Mon contenu",
id: 1234,
message: "Voici mon contenu",
author: {
name: "John"
},
comments: [
{
id: 45,
message: "Commentaire 1"
},
{
id: 46,
message: "Commentaire 2"
}
]
};
xxxxxxxxxx
// Using object literal notation
const person = {
name: 'John Doe',
age: 30,
occupation: 'Developer',
};
// Accessing object properties
console.log(person.name); // Output: John Doe
console.log(person.age); // Output: 30
console.log(person.occupation); // Output: Developer
xxxxxxxxxx
// JavaScript objects are a fundamental data structure used to represent various entities.
// They have properties that can be used to store key-value pairs of data.
// Create an object using object literal notation
const person = {
name: "John",
age: 25,
city: "New York"
};
// Accessing object properties
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 25
// Adding a new property to the object
person.gender = "Male";
// Updating an existing property
person.age = 26;
// Deleting a property
delete person.city;
// Looping through object properties
for (let key in person) {
console.log(key + ": " + person[key]);
}
// Output:
// name: John
// age: 26
// gender: Male