All technological notes.
Object
The values are written as name:value pairs (name and value separated by a colon).
It is a common practice to declare objects with the const keyword.
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue",
};
There are different ways to create new objects:
new.Object.create().console.log("\n-------- Object: create --------\n");
// Using an Object Literal
const person1 = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue",
};
console.log(person1);
// creates an empty JavaScript object, and then adds 4 properties
const person2 = {};
person2.firstName = "John";
person2.lastName = "Doe";
person2.age = 50;
person2.eyeColor = "blue";
console.log(person2);
// Using the JavaScript Keyword new
const person3 = new Object();
person3.firstName = "John";
person3.lastName = "Doe";
person3.age = 50;
person3.eyeColor = "blue";
console.log(person3);
properties.objectName.propertyNameobjectName["propertyName"]objectName.property;
objectName["property"];
objectName[expression]; // The expression must evaluate to a property name.
for...in statementfor (let variable in object) {
// code to be executed
}
Object.values() to return all properties’ valuesconst person = {
name: "John",
age: 30,
city: "New York",
hello: () => "hello",
};
console.log(Object.values(person)); //[ 'John', 30, 'New York', [Function: hello] ]
JSON.stringify() to convert properties and values to a string, except method.const person = {
name: "John",
age: 30,
city: "New York",
hello: () => "hello",
};
console.log(JSON.stringify(person)); //{"name":"John","age":30,"city":"New York"}
objectName.newProperty = value;
delete operator.delete objectName.property;
delete person["property"];
myObj = {
name: "John",
age: 30,
cars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat",
},
};
console.log(myObj.cars.car2); //BMW
console.log(myObj.cars["car2"]); //BMW
console.log(myObj["cars"]["car2"]); //BMW
Methods are actions that can be performed on objects.method is a function stored as a property.const person = {
firstName: "John",
lastName: "Doe",
id: 5566,
fullName: function () {
return this.firstName + " " + this.lastName; //this refers to the person object.
},
};
objectName.methodName();
objectName.methodName = function {
// method body
};
// define
function objectName(para, para, ...){
this.propertyName = para;
this.propertyName = para;
...
}
// call
const obj = new objectName(arg, arg, ...);
Built-in JavaScript Constructors JavaScript has built-in constructors for native objects:
new String(): A new String objectnew Number(): A new Number objectnew Boolean(): A new Boolean objectnew Object(): A new Object objectnew Array(): A new Array objectnew RegExp(): A new RegExp objectnew Function(): new Function objectnew Date(): A new Date objectExample:
console.log("\n-------- Object: constructor --------\n");
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");
console.log(myFather);
// Person {
// firstName: 'John',
// lastName: 'Doe',
// age: 50,
// eyeColor: 'blue'
// }
console.log(myMother);
// Person {
// firstName: 'Sally',
// lastName: 'Rally',
// age: 48,
// eyeColor: 'green'
// }
Objects are mutable: They are addressed by reference, not by value.
If person is an object, the following statement will not create a copy of person:
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue",
};
const x = person;
x.age = 10; // Will change both x.age and person.age
console.log(person); //{ firstName: 'John', lastName: 'Doe', age: 10, eyeColor: 'blue' }
The object x is not a copy of person. It is person. Both x and person are the same object.
Any changes to x will also change person, because x and person are the same object.
All JavaScript objects inherit properties and methods from a prototype:
Date objects inherit from Date.prototypeArray objects inherit from Array.prototypePerson objects inherit from Person.prototypeThe Object.prototype is on the top of the prototype inheritance chain:
Date objects, Array objects, and Person objects inherit from Object.prototype.
prototype Propertyconsole.log("\n-------- Object: constructor --------\n");
function Person(fname, lname) {
this.firstName = fname;
this.lastName = lname;
}
const p1 = new Person("John", "Doe");
console.log(p1.age); //undefined
// console.log(p1.fullname()); //TypeError: p1.fullname is not a function
Person.prototype.age = 18;
Person.prototype.name = function () {
return this.firstName + " " + this.lastName;
};
const p2 = new Person("Join", "Wick");
console.log(p1.age); //18
console.log(p1.name()); //John Doe
undefinednullif (typeof myObj !== "undefined" && myObj !== null) {
// operate object
}