Trong bài đăng này, chúng tôi sẽ xem xét vượt qua giá trị và vượt qua tham chiếu trong JavaScript.
Hãy cùng xem những gì được truyền qua giá trị và vượt qua tham chiếu trước khi xem xét bối cảnh JavaScript của nó.
Vượt qua giá trị:
Trong Pass by giá trị, hàm được gọi bằng cách truyền trực tiếp giá trị của biến làm đối số. Thay đổi đối số bên trong hàm không ảnh hưởng đến biến được truyền từ bên ngoài hàm.
JavaScript luôn vượt qua giá trị để thay đổi giá trị của biến không bao giờ thay đổi nguyên thủy cơ bản [chuỗi hoặc số]. so changing the value of the variable never changes the underlying primitive [String or number].
function callByValue[varOne, varTwo] {
console.log["Inside Call by Value Method"];
varOne = 100;
varTwo = 200;
console.log["varOne =" + varOne +"varTwo =" +varTwo];
} let varOne = 10;
let varTwo = 20; console.log["Before Call by Value Method"];
console.log["varOne =" + varOne +"varTwo =" +varTwo]; callByValue[varOne, varTwo] console.log["After Call by Value Method"];
console.log["varOne =" + varOne +"varTwo =" +varTwo];
output will be :
---------------
Before Call by Value Method
varOne =10 varTwo =20
Inside Call by Value Method
varOne =100 varTwo =200
After Call by Value Method
varOne =10 varTwo =20
Tuy nhiên, khi một biến đề cập đến một đối tượng bao gồm mảng, giá trị là tham chiếu đến đối tượng.
Vượt qua tham chiếu:
Trong tham chiếu vượt qua, hàm được gọi bằng cách truyền trực tiếp tham chiếu/địa chỉ của biến làm đối số. Thay đổi đối số bên trong hàm ảnh hưởng đến biến được truyền từ bên ngoài hàm. Trong các đối tượng và mảng JavaScript theo sau vượt qua tham chiếu.
function callByReference[varObj] {
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Vì vậy, nếu chúng ta chuyển đối tượng hoặc mảng làm đối số cho phương thức, thì có khả năng giá trị của đối tượng có thể thay đổi.
Nếu bạn muốn tạo trang web hoặc cổng thông tin - liên hệ với chúng tôi @ //www.bondesk.in
JavaScript được thiết kế trên một mô hình dựa trên đối tượng đơn giản. Một đối tượng là một tập hợp các thuộc tính và một thuộc tính là một liên kết giữa một tên [hoặc khóa] và một giá trị. Giá trị của một thuộc tính có thể là một hàm, trong trường hợp thuộc tính được gọi là phương thức. Ngoài các đối tượng được xác định trước trong trình duyệt, bạn có thể xác định các đối tượng của riêng mình. Chương này mô tả cách sử dụng các đối tượng, thuộc tính, chức năng và phương thức và cách tạo các đối tượng của riêng bạn. Các đối tượng trong JavaScript, giống như trong nhiều ngôn ngữ lập trình khác, có thể được so sánh với các đối tượng trong cuộc sống thực. Khái niệm về các đối tượng trong JavaScript có thể được hiểu với cuộc sống thực, các đối tượng hữu hình. Trong JavaScript, một đối tượng là một thực thể độc lập, với các thuộc tính và loại. So sánh nó với một cốc, ví dụ. Một cốc là một đối tượng, với các thuộc tính. Một cốc có màu, thiết kế, trọng lượng, một vật liệu được làm bằng, v.v ... Theo cùng một cách, các đối tượng JavaScript có thể có các thuộc tính, xác định các đặc điểm của chúng. Một đối tượng JavaScript có các thuộc tính liên quan đến nó. Một thuộc tính của một đối tượng có thể được giải thích là một biến được gắn vào đối tượng. Các thuộc tính đối tượng về cơ bản giống như các biến JavaScript thông thường, ngoại trừ phần đính kèm với các đối tượng. Các thuộc tính của một đối tượng xác định các đặc điểm của đối tượng. Bạn truy cập các thuộc tính của một đối tượng với một ghi chú dấu chấm đơn giản: Giống như tất cả các biến JavaScript, cả tên đối tượng [có thể là biến bình thường] và tên thuộc tính đều nhạy cảm. Bạn có thể xác định một thuộc tính bằng cách gán nó một giá trị. Ví dụ: chúng ta hãy tạo một đối tượng có tên Tổng quan đối tượng
Đối tượng và thuộc tính
7 và cung cấp cho nó các thuộc tính có tên const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
8, const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
9 và const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
0 như sau:myCar.color; // undefined
const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
Ví dụ trên cũng có thể được viết bằng trình khởi tạo đối tượng, là danh sách được phân phối bằng dấu phẩy bằng 0 hoặc nhiều cặp tên thuộc tính và giá trị liên quan của một đối tượng, được đặt trong niềng răng xoăn [
myCar.color; // undefined
1]:object initializer, which is a comma-delimited
list of zero or more pairs of property names and associated values of an object, enclosed in curly braces [myCar.color; // undefined
1]:const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
Các thuộc tính không được chỉ định của một đối tượng là
myCar.color; // undefined
2 [và không phải myCar.color; // undefined
3].myCar.color; // undefined
Thuộc tính của các đối tượng JavaScript cũng có thể được truy cập hoặc đặt bằng ký hiệu khung [để biết thêm chi tiết, hãy xem Trình truy cập thuộc tính]. Các đối tượng đôi khi được gọi là mảng liên kết, vì mỗi thuộc tính được liên kết với giá trị chuỗi có thể được sử dụng để truy cập nó. Vì vậy, ví dụ, bạn có thể truy cập các thuộc tính của đối tượng
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
7 như sau:myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
Tên thuộc tính đối tượng có thể là bất kỳ chuỗi JavaScript hợp lệ nào hoặc bất cứ thứ gì có thể được chuyển đổi thành một chuỗi, bao gồm một chuỗi trống. Tuy nhiên, bất kỳ tên thuộc tính nào không phải là định danh JavaScript hợp lệ đều không thể sử dụng ký hiệu DOT. Ví dụ: tên thuộc tính có không gian hoặc dấu gạch nối, bắt đầu bằng một số hoặc được giữ bên trong một biến chỉ có thể được truy cập bằng ký hiệu khung vuông. Ký hiệu này cũng rất hữu ích khi tên thuộc tính được xác định động, tức là không thể xác định cho đến khi chạy. Ví dụ như sau:
// four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
Tên thuộc tính đối tượng JavaScript [khóa] chỉ có thể là các chuỗi hoặc ký hiệu - tất cả các khóa trong ký hiệu khung vuông được chuyển đổi thành chuỗi trừ khi chúng là ký hiệu. Ví dụ: trong mã trên, khi khóa
myCar.color; // undefined
5 được thêm vào myCar.color; // undefined
6, JavaScript sẽ gọi phương thức myCar.color; // undefined
7 của myCar.color; // undefined
5 và sử dụng chuỗi kết quả làm khóa mới.Bạn cũng có thể truy cập các thuộc tính với giá trị chuỗi được lưu trữ trong một biến. Biến phải được truyền trong ký hiệu khung. Trong ví dụ trên, biến
myCar.color; // undefined
9 được giữ myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
0 và đó là myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
0 đó là tên thuộc tính. Do đó, myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
2 sẽ trở lại là không xác định.str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
Điều này cho phép truy cập bất kỳ thuộc tính nào như được xác định trong thời gian chạy:
let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
Bạn có thể sử dụng ký hiệu khung với
myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
3 để lặp lại trên tất cả các thuộc tính có thể suy giảm của một đối tượng. Để minh họa cách hoạt động của nó, hàm sau hiển thị các thuộc tính của đối tượng khi bạn chuyển tên đối tượng và tên của đối tượng làm đối số cho hàm:function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
Thuật ngữ "thuộc tính riêng" đề cập đến các thuộc tính của đối tượng, nhưng không bao gồm các thuộc tính của chuỗi nguyên mẫu. Vì vậy, chức năng gọi
myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
4 sẽ in như sau:function callByReference[varObj] {0
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Liệt kê các thuộc tính của một đối tượng
Có ba cách gốc để liệt kê/Traverse Object Properties:
3 vòng lặp. Phương pháp này đi qua tất cả các thuộc tính chuỗi có thể suy giảm của một đối tượng cũng như chuỗi nguyên mẫu của nó.myCar['make'] = 'Ford'; myCar['model'] = 'Mustang'; myCar['year'] = 1969;
6. Phương thức này trả về một mảng chỉ có tên thuộc tính chuỗi riêng ["phím"] trong đối tượngmyCar['make'] = 'Ford'; myCar['model'] = 'Mustang'; myCar['year'] = 1969;
6, nhưng không phải là trong chuỗi nguyên mẫu.myCar.color; // undefined
8. Phương thức này trả về một mảng chứa tất cả các tên thuộc tính chuỗi riêng trong đối tượngmyCar['make'] = 'Ford'; myCar['model'] = 'Mustang'; myCar['year'] = 1969;
6, bất kể chúng có thể không có được hay không.myCar.color; // undefined
Không có cách tự nhiên để liệt kê các thuộc tính "ẩn" [thuộc tính trong chuỗi nguyên mẫu không thể truy cập được thông qua đối tượng, bởi vì một thuộc tính khác có cùng tên trước đó trong chuỗi nguyên mẫu]. Tuy nhiên, điều này có thể đạt được với chức năng sau:
function callByReference[varObj] {1
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Tạo các đối tượng mới
JavaScript có một số đối tượng được xác định trước. Ngoài ra, bạn có thể tạo các đối tượng của riêng bạn. Bạn có thể tạo một đối tượng bằng Trình khởi tạo đối tượng. Ngoài ra, trước tiên bạn có thể tạo hàm tạo hàm và sau đó khởi tạo một đối tượng gọi hàm đó kết hợp với toán tử
// four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
0.Sử dụng các bộ khởi tạo đối tượng
Sử dụng các bộ khởi tạo đối tượng đôi khi được gọi là tạo các đối tượng có ký hiệu theo nghĩa đen. "Bộ khởi tạo đối tượng" phù hợp với thuật ngữ được sử dụng bởi C ++.
Cú pháp cho một đối tượng sử dụng Trình khởi tạo đối tượng là:
function callByReference[varObj] {2
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
trong đó
// four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
1 là tên của đối tượng mới, mỗi tên thuộc tính trước dấu chấm là một định danh [tên, một số hoặc một chuỗi theo nghĩa đen] và mỗi // four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
2 là một biểu thức có giá trị được gán cho tên thuộc tính. // four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
1 và phân công là tùy chọn; Nếu bạn không cần phải tham khảo đối tượng này ở nơi khác, bạn không cần phải gán nó cho một biến. .Bộ khởi tạo đối tượng là các biểu thức và mỗi trình khởi tạo đối tượng dẫn đến một đối tượng mới được tạo bất cứ khi nào câu lệnh xuất hiện được thực thi. Bộ khởi tạo đối tượng giống hệt nhau tạo ra các đối tượng riêng biệt sẽ không so sánh với nhau như nhau. Các đối tượng được tạo như thể một cuộc gọi đến
// four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
4 đã được thực hiện; Đó là, các đối tượng được làm từ các biểu thức theo nghĩa đen của đối tượng là các trường hợp của // four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
5.Câu lệnh sau đây tạo một đối tượng và gán nó cho biến
// four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
6 khi và chỉ khi biểu thức // four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
7 là đúng:function callByReference[varObj] {3
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Ví dụ sau đây tạo ra
// four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
8 với ba thuộc tính. Lưu ý rằng thuộc tính // four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
9 cũng là một đối tượng có thuộc tính riêng của nó.function callByReference[varObj] {4
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Bạn cũng có thể sử dụng bộ khởi tạo đối tượng để tạo mảng. Xem văn bản mảng.
Sử dụng hàm tạo hàm
Ngoài ra, bạn có thể tạo một đối tượng với hai bước sau:
- Xác định loại đối tượng bằng cách viết hàm tạo hàm. Có một quy ước mạnh mẽ, với lý do chính đáng, để sử dụng một lá thư ban đầu vốn.
- Tạo một thể hiện của đối tượng với
0.// four variables are created and assigned in a single go, // separated by commas const myObj = {}, str = 'myString', rand = Math.random[], anotherObj = {}; // Now, creating additional properties. myObj.type = 'Dot syntax for a key named type'; myObj['date created'] = 'This key has a space'; myObj[str] = 'This key is in variable str'; myObj[rand] = 'A random number is the key here'; myObj[anotherObj] = 'This key is object anotherObj'; myObj[''] = 'This key is an empty string'; console.log[myObj]; console.log[myObj.myString]; /* [Log] Object : "This key is an empty string" 0.8916485437228595: "A random number is the key here" [object Object]: "This key is object anotherObj" date created: "This key has a space" myString: "This key is in variable str" type: "Dot syntax for a key named type" */ // notice that in the log, the order of the properties listed is not the same as the order they were created. // [Log] This key is in variable str
Để xác định loại đối tượng, hãy tạo một hàm cho loại đối tượng chỉ định tên, thuộc tính và phương thức của nó. Ví dụ: giả sử bạn muốn tạo một loại đối tượng cho xe hơi. Bạn muốn loại đối tượng này được gọi là
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1 và bạn muốn nó có thuộc tính để tạo, mô hình và năm. Để làm điều này, bạn sẽ viết chức năng sau:function callByReference[varObj] {5
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Lưu ý rằng việc sử dụng
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
2 để gán các giá trị cho các thuộc tính của đối tượng dựa trên các giá trị được truyền cho hàm.Bây giờ bạn có thể tạo một đối tượng được gọi là
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
7 như sau:function callByReference[varObj] {6
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Câu lệnh này tạo ra
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
7 và gán nó các giá trị được chỉ định cho các thuộc tính của nó. Sau đó, giá trị của str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
5 là chuỗi "đại bàng", str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
6 là chuỗi 'Talon TSI', str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
7 là số nguyên năm 1993, v.v. Thứ tự của các đối số và tham số nên giống nhau.Bạn có thể tạo bất kỳ số lượng đối tượng
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1 bằng các cuộc gọi đến // four variables are created and assigned in a single go,
// separated by commas
const myObj = {},
str = 'myString',
rand = Math.random[],
anotherObj = {};
// Now, creating additional properties.
myObj.type = 'Dot syntax for a key named type';
myObj['date created'] = 'This key has a space';
myObj[str] = 'This key is in variable str';
myObj[rand] = 'A random number is the key here';
myObj[anotherObj] = 'This key is object anotherObj';
myObj[''] = 'This key is an empty string';
console.log[myObj];
console.log[myObj.myString];
/*
[Log] Object
: "This key is an empty string"
0.8916485437228595: "A random number is the key here"
[object Object]: "This key is object anotherObj"
date created: "This key has a space"
myString: "This key is in variable str"
type: "Dot syntax for a key named type"
*/
// notice that in the log, the order of the properties listed is not the same as the order they were created.
// [Log] This key is in variable str
0. Ví dụ,function callByReference[varObj] {7
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Một đối tượng có thể có một thuộc tính tự nó là một đối tượng khác. Ví dụ: giả sử bạn xác định một đối tượng được gọi là
let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
0 như sau:function callByReference[varObj] {8
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
và sau đó khởi tạo hai đối tượng
let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
0 mới như sau:function callByReference[varObj] {9
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Sau đó, bạn có thể viết lại định nghĩa của
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1 để bao gồm một thuộc tính let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
3 có đối tượng let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
0, như sau:const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
0Để khởi tạo các đối tượng mới, sau đó bạn sử dụng các đối tượng sau:
const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
1Lưu ý rằng thay vì truyền một chuỗi theo nghĩa đen hoặc giá trị số nguyên khi tạo các đối tượng mới, các câu lệnh trên truyền các đối tượng
let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
5 và let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
6 làm đối số cho chủ sở hữu. Sau đó, nếu bạn muốn tìm ra tên của chủ sở hữu của let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
7, bạn có thể truy cập thuộc tính sau:Lưu ý rằng bạn luôn có thể thêm một thuộc tính vào một đối tượng được xác định trước đó. Ví dụ: tuyên bố
Thêm một thuộc tính
let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
8 vào let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
9 và gán cho nó một giá trị function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
0. Tuy nhiên, điều này không ảnh hưởng đến bất kỳ đối tượng nào khác. Để thêm thuộc tính mới vào tất cả các đối tượng cùng loại, bạn phải thêm thuộc tính vào định nghĩa của loại đối tượng str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1.Sử dụng phương pháp function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
2
function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
Các đối tượng cũng có thể được tạo bằng phương pháp
function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
3. Phương pháp này có thể rất hữu ích, bởi vì nó cho phép bạn chọn đối tượng nguyên mẫu cho đối tượng bạn muốn tạo, mà không phải xác định hàm tạo hàm.const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
2Di sản
Tất cả các đối tượng trong JavaScript kế thừa từ ít nhất một đối tượng khác. Đối tượng được di truyền từ được gọi là nguyên mẫu và các thuộc tính được di truyền có thể được tìm thấy trong đối tượng
function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
4 của hàm tạo. Xem kế thừa và chuỗi nguyên mẫu để biết thêm thông tin.Lập chỉ mục các thuộc tính đối tượng
Bạn có thể tham khảo một thuộc tính của một đối tượng bằng tên thuộc tính của nó hoặc theo chỉ số thứ tự của nó. Nếu ban đầu bạn xác định một thuộc tính bằng tên của nó, bạn phải luôn tham khảo nó bằng tên của nó và nếu ban đầu bạn xác định một thuộc tính theo chỉ mục, bạn phải luôn luôn tham khảo nó bằng chỉ mục của nó.
Hạn chế này áp dụng khi bạn tạo một đối tượng và các thuộc tính của nó với hàm tạo hàm [như chúng tôi đã làm trước đây với loại đối tượng
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1] và khi bạn xác định rõ ràng các thuộc tính riêng lẻ [ví dụ: function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
6]. Nếu ban đầu bạn xác định một thuộc tính đối tượng có chỉ mục, chẳng hạn như function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
7, sau đó bạn chỉ tham khảo thuộc tính này là function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
8.Ngoại lệ cho quy tắc này là các đối tượng giống như mảng được phản ánh từ HTML, chẳng hạn như đối tượng giống như mảng
function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
9. Bạn luôn có thể tham khảo các đối tượng trong các đối tượng giống như mảng này bằng số thứ tự của chúng [dựa trên nơi chúng xuất hiện trong tài liệu] hoặc tên của chúng [nếu được xác định]. Ví dụ: nếu thẻ function callByReference[varObj] {00 thứ hai trong tài liệu có thuộc tính
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {01, bạn có thể gọi biểu mẫu là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {02 hoặc
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {03 hoặc
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {04.
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Xác định thuộc tính cho loại đối tượng
Bạn có thể thêm thuộc tính vào loại đối tượng được xác định trước đó bằng cách sử dụng thuộc tính
function showProps[obj, objName] {
let result = '';
for [const i in obj] {
// obj.hasOwn is used to exclude properties from the object's prototype chain and only show "own properties"
if [Object.hasOwn[obj, i]] {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log[result];
}
4. Điều này xác định một thuộc tính được chia sẻ bởi tất cả các đối tượng thuộc loại được chỉ định, thay vì chỉ bằng một thể hiện của đối tượng. Mã sau đây thêm thuộc tính let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
8 cho tất cả các đối tượng thuộc loại str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1, sau đó gán một giá trị cho thuộc tính let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
8 của đối tượng let propertyName = 'make';
myCar[propertyName] = 'Ford';
// access different properties by changing the contents of the variable
propertyName = 'model';
myCar[propertyName] = 'Mustang';
console.log[myCar];
// [Log] {make: 'Ford', model: 'Mustang'}
9.const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
3Xem kế thừa_and_the_prototype_chain để biết thêm thông tin.
Xác định phương pháp
Phương pháp là một hàm được liên kết với một đối tượng, hoặc, khác nhau, một phương thức là một thuộc tính của một đối tượng là một hàm. Các phương thức được xác định theo cách xác định các hàm bình thường, ngoại trừ việc chúng phải được gán là thuộc tính của một đối tượng. Xem thêm Định nghĩa phương pháp để biết thêm chi tiết. Một ví dụ là:
const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
4Trong đó
function callByReference[varObj] {10 là một đối tượng hiện có,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {11 là tên bạn đang gán cho phương thức và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {12 là tên của hàm.
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Sau đó, bạn có thể gọi phương thức trong bối cảnh của đối tượng như sau:
const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
5Bạn có thể xác định các phương thức cho một loại đối tượng bằng cách bao gồm một định nghĩa phương thức trong hàm xây dựng đối tượng. Bạn có thể xác định một hàm sẽ định dạng và hiển thị các thuộc tính của các đối tượng
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1 được xác định trước đó; Ví dụ,const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
6trong đó
function callByReference[varObj] {14 là một hàm để hiển thị quy tắc ngang và chuỗi. Lưu ý việc sử dụng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
2 để tham khảo đối tượng mà phương thức thuộc về.Bạn có thể biến hàm này thành một phương thức của
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1 bằng cách thêm câu lệnhconst myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
7đến định nghĩa đối tượng. Vì vậy, định nghĩa đầy đủ của
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
1 bây giờ sẽ trông giống nhưconst myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
8Sau đó, bạn có thể gọi phương thức
function callByReference[varObj] {18 cho từng đối tượng như sau:
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
const myCar = new Object[];
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
9Sử dụng str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
2 cho các tài liệu tham khảo đối tượng
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
JavaScript có một từ khóa đặc biệt,
str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
2, mà bạn có thể sử dụng trong một phương thức để chỉ đối tượng hiện tại. Ví dụ: giả sử bạn có 2 đối tượng, function callByReference[varObj] {21 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {22. Mỗi đối tượng có
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {23,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {24 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {25 của riêng họ. Trong hàm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {26, thông báo có
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {27. Khi được thêm vào 2 đối tượng, chúng có thể được gọi và in
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {28 sau đó thêm giá trị
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {23 từ đối tượng cụ thể đó. Như hình dưới đây.
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
0str = 'myString';
myObj[str] = 'This key is in variable str';
console.log[myObj.str]; //[Log] undefined
console.log[myObj[str]]; //[Log] This key is in variable str
console.log[myObj.myString]; //[Log] This key is in variable str
2 đề cập đến đối tượng mà nó đang ở. Bạn có thể tạo một chức năng mới gọi là function callByReference[varObj] {31 đăng nhập một câu cho biết người đó bao nhiêu tuổi.
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
1Xác định getters và setters
Getter là một phương pháp nhận được giá trị của một thuộc tính cụ thể. Một setter là một phương thức đặt giá trị của một thuộc tính cụ thể. Bạn có thể xác định getters và setters trên bất kỳ đối tượng lõi được xác định trước hoặc đối tượng do người dùng xác định hỗ trợ bổ sung các thuộc tính mới.
Getters và setters có thể là
- được xác định bằng cách sử dụng khởi tạo đối tượng hoặc
- Đã thêm sau vào bất kỳ đối tượng nào bất cứ lúc nào bằng phương thức thêm getter hoặc setter.
Khi xác định getters và setters sử dụng bộ khởi tạo đối tượng, tất cả những gì bạn cần làm là tiền tố phương thức getter với
function callByReference[varObj] {32 và phương thức setter với
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {33. Tất nhiên, phương thức Getter không được mong đợi một tham số, trong khi phương thức Setter mong đợi chính xác một tham số [giá trị mới để đặt]. Ví dụ:
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
2Các thuộc tính của đối tượng
myCar.color; // undefined
6 là:function callByReference[varObj] {
35 - một số
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
36 - một người nhận trả về
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
35 cộng với 1
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
38 - Một setter đặt giá trị của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
35 thành một nửa giá trị
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
38 đang được đặt thành
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Xin lưu ý rằng tên hàm của getters và setters được xác định trong một đối tượng theo nghĩa đen bằng cách sử dụng "[gs] et property []" không phải là tên của chính các getters, mặc dù cú pháp
function callByReference[varObj] {41 có thể đánh lừa bạn nghĩ khác.
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Getters và setters cũng có thể được thêm vào một đối tượng bất cứ lúc nào sau khi tạo bằng phương pháp
function callByReference[varObj] {42. Tham số đầu tiên của phương thức này là đối tượng mà bạn muốn xác định getter hoặc setter. Tham số thứ hai là một đối tượng có tên thuộc tính là tên getter hoặc setter và có giá trị thuộc tính là đối tượng để xác định các hàm getter hoặc setter. Dưới đây là một ví dụ xác định cùng một getter và setter được sử dụng trong ví dụ trước:
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
3Những hình thức nào trong hai hình thức để chọn phụ thuộc vào phong cách lập trình và nhiệm vụ của bạn trong tay. Nếu bạn đã sử dụng trình khởi tạo đối tượng khi xác định nguyên mẫu, bạn có thể sẽ chọn biểu mẫu đầu tiên. Hình thức này nhỏ gọn và tự nhiên hơn. Tuy nhiên, nếu bạn cần thêm getters và setters sau - vì bạn không viết nguyên mẫu hoặc đối tượng cụ thể - thì biểu mẫu thứ hai là biểu mẫu duy nhất có thể. Hình thức thứ hai có thể thể hiện tốt nhất bản chất năng động của JavaScript - nhưng nó có thể làm cho mã khó đọc và hiểu.
Xóa thuộc tính
Bạn có thể xóa một thuộc tính không được inher bằng cách sử dụng toán tử
function callByReference[varObj] {43. Mã sau đây cho thấy cách xóa một thuộc tính.
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
4So sánh các đối tượng
Trong JavaScript, các đối tượng là một loại tham chiếu. Hai đối tượng riêng biệt không bao giờ bằng nhau, ngay cả khi chúng có cùng thuộc tính. Chỉ so sánh cùng một tham chiếu đối tượng với chính nó mang lại đúng.
const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
5const myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
};
6Để biết thêm thông tin về các nhà khai thác so sánh, hãy xem các nhà khai thác bình đẳng.
Xem thêm
- Để lặn sâu hơn, hãy đọc về kế thừa và chuỗi nguyên mẫu.
- Để tìm hiểu về các lớp [một cách khác để tạo các đối tượng], hãy đọc tham chiếu của các lớp JavaScript.
- " Trước
- Tiếp theo "