Chúng tôi tiếp tục với loạt bài của mình để giúp bạn chuẩn bị tốt nhất có thể cho các cuộc phỏng vấn trong Javascript. Chúng tôi sẽ xem xét một câu hỏi khác mà tôi đã được đưa ra trong một cuộc phỏng vấn cho vị trí nhà phát triển front-end cấp cao trong một công ty khởi nghiệp
triển khai chức năng liên kết của riêng bạn trong vanilla javascript
Điều cần thiết là phải hiểu câu hỏi trước khi vội vàng trả lời. Tôi thực sự khuyên bạn nên đọc bài đăng này về quá trình phỏng vấn
Vui lòng viết một hàm liên kết trả về một hàm mới như định nghĩa sau
Phương thức bind[] tạo một hàm mới, khi được gọi, từ khóa this của nó được đặt thành giá trị được cung cấp, với một chuỗi đối số đã cho trước bất kỳ hàm nào được cung cấp khi hàm mới được gọi
Chúng ta sẽ cần thực hiện các bước sau để viết chức năng của mình
- Kiểm tra xem hàm liên kết có được áp dụng cho một hàm không
- Đầu vào thứ hai phải là ngữ cảnh “cái này” để liên kết với
- Xử lý các đối số chức năng đầu vào
- trả lại một chức năng mới
- kiểm tra, kiểm tra và kiểm tra
Xác thực đầu vào
Chúng tôi sẽ sử dụng hàm typeof để xác thực rằng đầu vào thực tế là một hàm
const ourBind = function [thisContext, bindFunction] {
if [typeof bindFunction !== 'function'] {
throw new Error['You can only bind to a function']
}
Bây giờ chúng ta nên trả về một chức năng mới
const ourBind = function [thisContext, bindFunction] {
if [typeof this !== 'function'] {
throw new Error['You can only bind to a function']
}
return function[]{ }
}
Tiếp theo, chúng ta nên gọi hàm được yêu cầu với các đối số của nó
const ourBind = function [thisContext, bindFunction] {
if [typeof this !== 'function'] {
throw new Error['You can only bind to a function']
}
return function[]{
bindFunction[arguments]
}
}
Chức năng mới bây giờ sẽ được gọi khi được yêu cầu nhưng vẫn không có ràng buộc thực sự
Đối số Javascript
mozilla
arguments
là một đối tượng giống nhưArray
có thể truy cập bên trong các hàm có chứa các giá trị của các đối số được truyền cho hàm đó
“Đối số” là một đối tượng giống như mảng, nghĩa là nó không phải là một mảng nên chúng ta sẽ ép nó thành một
const ourBind = function [thisContext, bindFunction] {
if [typeof this !== 'function'] {
throw new Error['You can only bind to a function']
}
return function[]{
bindFunction[Array.from[arguments]]
}
}
Áp dụng Javascript
Phương thức
apply[]
gọi hàm đã chỉ định với một giá trị0 đã cho vàconst ourBind = function [thisContext, bindFunction] { if [typeof this !== 'function'] { throw new Error['You can only bind to a function'] } return function[]{ } }
arguments
được cung cấp dưới dạng một mảng [hoặc một ]mozilla
Chơi lô tô. Bằng cách kết hợp Áp dụng và truyền đối số cho một mảng, giờ đây chúng ta có thể hoàn thiện hàm liên kết của riêng mình
const ourBind = function [thisContext, bindFunction] {
if [typeof this !== 'function'] {
throw new Error['You can only bind to a function']
}
return function[]{
bindFunction.apply[thisContext, Array.from[arguments]]
}
}
Cuộc gọi thông báo cũng là một cách thay thế để áp dụng nhưng nó nhận từng đối số một thay vì một mảng sẽ thuận tiện hơn trong trường hợp này. Nếu bạn chọn sử dụng chức năng gọi thì bạn sẽ cần trải rộng các đối số
Trong bài viết này, tôi sẽ giải thích cách sử dụng lệnh gọi, áp dụng và liên kết trong JavaScript bằng các ví dụ đơn giản
Chúng tôi cũng sẽ triển khai một ví dụ minh họa cách bạn có thể tạo chức năng bản đồ của riêng mình bằng chức năng áp dụng
Không chần chừ thêm nữa, chúng ta hãy bắt đầu
Mục lục
điều kiện tiên quyết
Dưới đây là một số điều bạn nên hiểu để tận dụng tối đa bài viết này
- Chức năng
- Nguyên mẫu chức năng
- từ khóa này
Định nghĩa
Hãy xem xét các hàm mà chúng ta sẽ nghiên cứu ở đây kỹ hơn một chút để hiểu chức năng của chúng
Gọi là một chức năng giúp bạn thay đổi ngữ cảnh của chức năng gọi. Theo thuật ngữ của giáo dân, nó giúp bạn thay thế giá trị của
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 bên trong một hàm bằng bất kỳ giá trị nào bạn muốnÁp dụng rất giống với chức năng
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
8. Sự khác biệt duy nhất là trong function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9 bạn có thể truyền một mảng dưới dạng danh sách đối sốBind là một chức năng giúp bạn tạo một chức năng khác mà bạn có thể thực thi sau này với bối cảnh mới là
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 được cung cấpBây giờ chúng ta sẽ xem xét một số ví dụ cơ bản về các hàm gọi, áp dụng và liên kết. Sau đó, chúng ta sẽ xem xét một ví dụ mà chúng ta sẽ xây dựng chức năng của riêng mình tương tự như chức năng bản đồ
Cách sử dụng hàm gọi trong JavaScript
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
8 là một hàm mà bạn sử dụng để thay đổi giá trị của function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 bên trong một hàm và thực thi nó với các đối số được cung cấpĐây là cú pháp của hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
8
func.call[thisObj, args1, args2, ...]
Ở đâu,
- func là một chức năng cần được gọi với một đối tượng
7 khácfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- thisObj là một đối tượng hoặc một giá trị cần được thay thế bằng từ khóa
7 có bên trong hàmfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
6const newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
- args1, args2 là các đối số được chuyển đến hàm gọi với đối tượng
7 đã thay đổifunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
Lưu ý rằng nếu bạn gọi một hàm mà không có bất kỳ đối số
const newEntity = [obj] => console.log[obj];
function mountEntity[]{
this.entity = newEntity;
console.log[`Entity ${this.entity} is mounted on ${this}`];
}
mountEntity.call[];
8 nào, thì JavaScript sẽ coi thuộc tính này là một đối tượng toàn cụcBây giờ chúng ta đã có một số ngữ cảnh xung quanh hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
8 là gì, hãy bắt đầu bằng cách hiểu nó chi tiết hơn với một số ví dụCách gọi một hàm với các ngữ cảnh khác nhau trong JS
Xem xét ví dụ dưới đây. Nó bao gồm 3 lớp –
func.apply[thisObj, argumentsArray];
0, func.apply[thisObj, argumentsArray];
1 và func.apply[thisObj, argumentsArray];
2function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
Nếu để ý kỹ, bạn có thể thấy rằng chúng ta sử dụng hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
8 để gọi hàm func.apply[thisObj, argumentsArray];
0 trong hai trường hợp. Đầu tiên, trong hàm func.apply[thisObj, argumentsArray];
5 và sau đó trong hàm func.apply[thisObj, argumentsArray];
6Trong cả hai hàm này, chúng ta gọi hàm
func.apply[thisObj, argumentsArray];
0 với đối tượng function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 đại diện cho chính các hàm tương ứng. Ví dụ, bên trong func.apply[thisObj, argumentsArray];
5, chúng ta gọi hàm func.apply[thisObj, argumentsArray];
0 với đối tượng function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 thuộc ngữ cảnh của nó. Trường hợp tương tự đối với func.apply[thisObj, argumentsArray];
6Cách gọi một hàm không có đối số trong JS
Xem xét ví dụ dưới đây
const newEntity = [obj] => console.log[obj];
function mountEntity[]{
this.entity = newEntity;
console.log[`Entity ${this.entity} is mounted on ${this}`];
}
mountEntity.call[];
Trong ví dụ này, chúng tôi đã gọi hàm
func.apply[thisObj, [args1, args2, ...]];
3 mà không có đối số const newEntity = [obj] => console.log[obj];
function mountEntity[]{
this.entity = newEntity;
console.log[`Entity ${this.entity} is mounted on ${this}`];
}
mountEntity.call[];
8. Trong những trường hợp như vậy, JavaScript đề cập đến đối tượng toàn cầuCách sử dụng chức năng Áp dụng trong JavaScript
Hàm
func.apply[thisObj, [args1, args2, ...]];
5 rất giống với hàm func.apply[thisObj, [args1, args2, ...]];
6. Sự khác biệt duy nhất giữa function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
8 và function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9 là sự khác biệt trong cách truyền đối sốTrong
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9, các đối số bạn có thể truyền đối số dưới dạng một mảng ký tự hoặc một đối tượng mảng mớiĐây là cú pháp của hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9func.apply[thisObj, argumentsArray];
Ở đâu,
- func là một chức năng cần được gọi với một đối tượng
7 khácfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- thisObj là một đối tượng hoặc một giá trị cần được thay thế bằng từ khóa
7 có bên trong hàmfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
6const newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
- đối sốArray có thể là một mảng đối số, đối tượng mảng hoặc chính từ khóa đối số
Như bạn có thể thấy ở trên, hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9 có các loại cú pháp khác nhauCú pháp đầu tiên là một cú pháp đơn giản. Bạn có thể chuyển vào một mảng các đối số như bên dưới
func.apply[thisObj, [args1, args2, ...]];
Cú pháp thứ hai là nơi chúng ta có thể truyền vào đối tượng mảng mới cho nó
func.apply[thisObj, new Array[args1, args2]];
Cú pháp thứ ba là nơi chúng ta có thể chuyển vào từ khóa đối số
func.apply[thisObj, arguments];
đối số là một đối tượng đặc biệt có sẵn bên trong một chức năng. Nó chứa các giá trị của các đối số được truyền cho một hàm. Bạn có thể sử dụng từ khóa này với hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9 để lấy bất kỳ số lượng đối số tùy ý nàoPhần hay nhất về
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9 là chúng ta không cần quan tâm đến số lượng đối số được truyền cho hàm gọi. Do tính chất năng động và linh hoạt của nó, bạn có thể sử dụng nó trong các tình huống phức tạpHãy xem ví dụ tương tự như trên, nhưng lần này chúng ta sẽ sử dụng hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
9function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
Và đây là một ví dụ giới thiệu cách bạn sử dụng từ khóa
func.apply[thisObj, new Array[args1, args2]];
8function addUp[]{
//Using arguments to capture the arbitrary number of inputs
const args = Array.from[arguments];
this.x = args.reduce[[prev, curr] => prev + curr, 0];
console.log["this.x = ", this.x];
}
function driverFunc[]{
const obj = {
inps: [1,2,3,4,5,6]
}
addUp.apply[obj, obj.inps];
}
driverFunc[];
Cách sử dụng chức năng liên kết trong JavaScript
Hàm
func.apply[thisObj, new Array[args1, args2]];
9 tạo một bản sao của hàm có giá trị mới cho ____6_______7 hiện diện bên trong hàm gọiĐây là cú pháp của hàm
func.apply[thisObj, new Array[args1, args2]];
9func.bind[thisObj, arg1, arg2, ..., argN];
Ở đâu,
- func là một chức năng cần được gọi với một đối tượng
7 khácfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- thisObj là một đối tượng hoặc một giá trị cần được thay thế bằng từ khóa
7 có bên trong hàmfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
6const newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
- arg1, arg2…argN – bạn có thể truyền 1 đối số cho hàm gọi hoặc nhiều hơn thế, tương tự như hàm
8function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
Hàm
func.apply[thisObj, new Array[args1, args2]];
9 sau đó trả về một hàm mới bao gồm ngữ cảnh mới cho biến function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 có bên trong hàm gọifunction Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
0Bây giờ chức năng này
const newEntity = [obj] => console.log[obj];
function mountEntity[]{
this.entity = newEntity;
console.log[`Entity ${this.entity} is mounted on ${this}`];
}
mountEntity.call[];
6 có thể được thực thi sau với các đối sốHãy xem một ví dụ kinh điển về cách sử dụng hàm
func.apply[thisObj, new Array[args1, args2]];
9 với sự trợ giúp của thành phần React dựa trên lớpfunction Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
1Xem xét thành phần Ứng dụng ở trên. Nó cấu thành những điều sau đây
0 là một hàm được gọi là một lớp và được khởi tạo bằng từ khóafunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
1function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
2 là một hàm thực thi/kết xuất mã JSXfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
3 là một phương thức lớp ghi lại trạng thái của thành phầnfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
Nếu chúng tôi nhấp vào nút
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
4 thì chúng tôi sẽ nhận được thông báo lỗi. function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
5Bạn đã bao giờ tự hỏi tại sao vấn đề này xảy ra?
Bạn có thể mong đợi rằng chúng ta có thể truy cập trạng thái của lớp vì
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
3 là một phương thức của lớp. Nhưng đây là bắt
7 bên trongfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
3 không giống với lớp củafunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
7function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- Bên trong một lớp,
7 là một đối tượng thông thường có các phương thức lớp không tĩnh làm thuộc tính của nó. Nhưngfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
7 bên trongfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
3 sẽ đề cập đến một ngữ cảnh khácfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- Thành thật mà nói, giá trị của
7 trong trường hợp này phụ thuộc vào nơi các chức năng được gọi. Nếu bạn thấy,function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
3 đang được gọi trong sự kiệnfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
5function addUp[]{ //Using arguments to capture the arbitrary number of inputs const args = Array.from[arguments]; this.x = args.reduce[[prev, curr] => prev + curr, 0]; console.log["this.x = ", this.x]; } function driverFunc[]{ const obj = { inps: [1,2,3,4,5,6] } addUp.apply[obj, obj.inps]; } driverFunc[];
- Nhưng ở giai đoạn này, chúng ta sẽ nhận được
6 cho bối cảnh củafunction addUp[]{ //Using arguments to capture the arbitrary number of inputs const args = Array.from[arguments]; this.x = args.reduce[[prev, curr] => prev + curr, 0]; console.log["this.x = ", this.x]; } function driverFunc[]{ const obj = { inps: [1,2,3,4,5,6] } addUp.apply[obj, obj.inps]; } driverFunc[];
7 hiện diện bên trong hàmfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
3function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- Chúng tôi đang cố gọi thuộc tính
9 có giá trị không xác định. Do đó, điều này dẫn đến lỗi trênfunction addUp[]{ //Using arguments to capture the arbitrary number of inputs const args = Array.from[arguments]; this.x = args.reduce[[prev, curr] => prev + curr, 0]; console.log["this.x = ", this.x]; } function driverFunc[]{ const obj = { inps: [1,2,3,4,5,6] } addUp.apply[obj, obj.inps]; } driverFunc[];
Chúng ta có thể khắc phục điều này bằng cách cung cấp ngữ cảnh phù hợp của
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 bên trong phương thức function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
3. Bạn có thể làm điều này với phương pháp func.apply[thisObj, new Array[args1, args2]];
9function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
2func.apply[thisObj, new Array[args1, args2]];
9 sẽ tạo một hàm mới và lưu trữ nó bên trong đối tượng function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 với một thuộc tính mới là function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
3. func.bind[thisObj, arg1, arg2, ..., argN];
6 sẽ đảm bảo rằng ngữ cảnh function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 của lớp được áp dụng cho hiện tại của function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
7 bên trong hàm function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.apply[this, ["convertible", "petrol"]]; //Syntax with array literal
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.apply[this, new Array["convertible", "diesel"]]; //Syntax with array object construction
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
3Cách tạo Hàm function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
00 của riêng bạn
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
Bây giờ chúng ta đã có tất cả những thứ cần thiết, hãy bắt đầu bằng cách tạo chức năng bản đồ
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
01 của chúng ta. Trước tiên hãy hiểu những thứ mà chúng ta sẽ cần để xây dựng chức năng bản đồ function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
01 của mìnhĐây là cú pháp của hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
00function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
3Ở đâu,
- mảng là một mảng mà bản đồ được gọi
- func là hàm cần chạy trên từng phần tử của mảng
Chức năng cơ bản của hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
00 rất đơn giảnĐó là một hàm đi qua từng phần tử của một mảng và áp dụng hàm được truyền dưới dạng đối số. Kiểu trả về của bản đồ lại là một mảng với
const newEntity = [obj] => console.log[obj];
function mountEntity[]{
this.entity = newEntity;
console.log[`Entity ${this.entity} is mounted on ${this}`];
}
mountEntity.call[];
6 được áp dụng cho mỗi phần tửBây giờ chúng ta đã hiểu các yêu cầu, vì vậy chúng ta có thể chuyển sang tạo hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
00 của riêng mình. Đây là mã của chức năng function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
00 mới của chúng tôifunction Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
4Hãy hiểu chức năng trên từng chút một
- Hàm này chấp nhận một đối số có tên là
6. Nó chẳng là gì ngoài một hàm cần được gọi trên mỗi phần tử của một mảngconst newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
- Các phần khác của mã khá tự giải thích. Chúng tôi sẽ tập trung vào dòng sau.
09function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- Dòng này làm hai việc
1. Đẩy các thay đổi vào
10function Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
2. Thực hiện
6 với sự trợ giúp của phương phápconst newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
8. Ở đây, phương thứcfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
8 [như đã giải thích trong các phần trước] sẽ thực thi phương thứcfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
6 với một giá trị mới cho đối tượngconst newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
7 có trong phương thứcfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
6const newEntity = [obj] => console.log[obj]; function mountEntity[]{ this.entity = newEntity; console.log[`Entity ${this.entity} is mounted on ${this}`]; } mountEntity.call[];
Bây giờ hãy xem cách chúng ta sẽ thực thi hàm
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
17. Cách tiếp cận dưới đây để thêm một phương thức mới vào kiểu dữ liệu nguyên thủy hiện có không được khuyến nghị nhưng chúng tôi vẫn sẽ làm điều đó vì mục đích của bài viết nàyGHI CHÚ. không làm theo cách tiếp cận dưới đây trong mã sản xuất của bạn. Điều này có thể gây ra thiệt hại cho mã hiện có
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
5function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
18 chúng tôi tạo một thuộc tính mới bên trong function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
19Khi điều này được thực hiện, chúng ta có thể bắt đầu thực hiện chức năng bản đồ mới của mình trên một mảng
function Car[type, fuelType]{
this.type = type;
this.fuelType = fuelType;
}
function setBrand[brand]{
Car.call[this, "convertible", "petrol"];
this.brand = brand;
console.log[`Car details = `, this];
}
function definePrice[price]{
Car.call[this, "convertible", "diesel"];
this.price = price;
console.log[`Car details = `, this];
}
const newBrand = new setBrand['Brand1'];
const newCarPrice = new definePrice[100000];
6Tóm lược
Bài viết này đã cho bạn thấy chức năng gọi, áp dụng và liên kết có thể thực hiện thông qua các ví dụ
Vì vậy, để nói về các chức năng này một cách ngắn gọn
- Gọi, áp dụng và liên kết là các hàm giúp bạn thay đổi ngữ cảnh của từ khóa
7 có trong hàm gọifunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- Chúng ta đã thấy mỗi hàm có thể được gọi theo những cách khác nhau như thế nào – ví dụ: với
9 bạn có thể thực thi một hàm với một mảng đối số và với hàmfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
8 bạn có thể thực hiện tương tự nhưng các đối số được phân bổ bằng dấu phẩyfunction Car[type, fuelType]{ this.type = type; this.fuelType = fuelType; } function setBrand[brand]{ Car.call[this, "convertible", "petrol"]; this.brand = brand; console.log[`Car details = `, this]; } function definePrice[price]{ Car.call[this, "convertible", "diesel"]; this.price = price; console.log[`Car details = `, this]; } const newBrand = new setBrand['Brand1']; const newCarPrice = new definePrice[100000];
- Các chức năng này thực sự hữu ích trong các thành phần dựa trên lớp của React
Cảm ơn bạn đã đọc
Theo dõi tôi trên Twitter, GitHub và LinkedIn
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Nhà phát triển front-end👨💻;
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có việc làm với tư cách là nhà phát triển. Bắt đầu