Hướng dẫn which of the following is used for calling a function or method in javascript? - cách nào sau đây được sử dụng để gọi một hàm hoặc phương thức trong javascript?

Phương thức call() gọi hàm có giá trị this và đối số đã cho được cung cấp riêng lẻ.call() method calls the function with a given this value and arguments provided individually.

Thử nó

Cú pháp

call()
call(thisArg)
call(thisArg, arg1, /* …, */ argN)

Thông số

thisArg

Giá trị để sử dụng là this khi gọi

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
0. Nếu hàm không ở chế độ nghiêm ngặt,
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
1 và
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
2 sẽ được thay thế bằng đối tượng toàn cầu và các giá trị nguyên thủy sẽ được chuyển đổi thành các đối tượng.

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
3 Tùy chọnOptional

Đối số cho chức năng.

Giá trị trả về

Kết quả của việc gọi hàm với giá trị và đối số this được chỉ định.

Sự mô tả

Lưu ý: Hàm này gần như giống hệt với

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
5, ngoại trừ call() chấp nhận danh sách đối số, trong khi
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
5 chấp nhận một mảng đối số duy nhất - ví dụ:
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
8 so với
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
9.
This function is almost identical to
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
5, except that call() accepts an argument list, while
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
5 accepts a single array of arguments — for example,
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
8 vs.
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
9.

call() cho phép một hàm/phương thức thuộc về một đối tượng được gán và gọi cho một đối tượng khác.

call() cung cấp giá trị mới của this cho hàm/phương thức. Với call(), bạn có thể viết một phương thức một lần và sau đó kế thừa nó trong một đối tượng khác mà không phải viết lại phương thức cho đối tượng mới.

Ví dụ

Sử dụng call () cho các hàm tạo chuỗi cho một đối tượng

Bạn có thể sử dụng

const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
4 cho các hàm tạo chuỗi cho một đối tượng (tương tự như Java).

Trong ví dụ sau, hàm tạo cho đối tượng

const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
5 được xác định với hai tham số:
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
6 và
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
7.

Hai chức năng khác,

const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
8 và
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
9, gọi
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
5, vượt qua this,
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
6 và
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
7.
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
5 Khởi tạo các thuộc tính
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
6 và
const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
7, cả hai hàm chuyên dụng xác định
function greet() {
  const reply = [this.animal, 'typically sleep between', this.sleepDuration].join(' ');
  console.log(reply);
}

const obj = {
  animal: 'cats',
  sleepDuration: '12 and 16 hours',
};

greet.call(obj);  // cats typically sleep between 12 and 16 hours
7.

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);

Sử dụng call () để gọi một hàm ẩn danh

Trong ví dụ này, chúng tôi tạo một hàm ẩn danh và sử dụng

const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}
4 để gọi nó trên mọi đối tượng trong một mảng.

Mục đích chính của hàm ẩn danh ở đây là thêm hàm

function greet() {
  const reply = [this.animal, 'typically sleep between', this.sleepDuration].join(' ');
  console.log(reply);
}

const obj = {
  animal: 'cats',
  sleepDuration: '12 and 16 hours',
};

greet.call(obj);  // cats typically sleep between 12 and 16 hours
9 cho mọi đối tượng, có thể in đúng chỉ mục của đối tượng trong mảng.

Lưu ý: Truyền đối tượng là giá trị this không hoàn toàn cần thiết, nhưng được thực hiện cho mục đích giải thích. Passing the object as this value is not strictly necessary, but is done for explanatory purpose.

const animals = [
  { species: 'Lion', name: 'King' },
  { species: 'Whale', name: 'Fail' }
];

function assignPrintMethod(i) {
  this.print = function () {
    console.log(`#${i} ${this.species}: ${this.name}`);
  }
  this.print();
}

for (let i = 0; i < animals.length; i++) {
  assignPrintMethod.call(animals[i], i);
}

Sử dụng call () để gọi một hàm và chỉ định ngữ cảnh cho 'cái này'

Trong ví dụ dưới đây, khi chúng ta gọi

// var creates a property on the global object
var globProp = 'Wisen';

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
1, giá trị của this sẽ bị ràng buộc với đối tượng
// var creates a property on the global object
var globProp = 'Wisen';

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
3.

function greet() {
  const reply = [this.animal, 'typically sleep between', this.sleepDuration].join(' ');
  console.log(reply);
}

const obj = {
  animal: 'cats',
  sleepDuration: '12 and 16 hours',
};

greet.call(obj);  // cats typically sleep between 12 and 16 hours

Sử dụng call () để gọi một hàm và không chỉ định đối số đầu tiên

Trong ví dụ dưới đây, chúng tôi gọi hàm

// var creates a property on the global object
var globProp = 'Wisen';

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
4 mà không chuyển đối số đầu tiên. Nếu đối số đầu tiên không được thông qua, giá trị của this bị ràng buộc với đối tượng toàn cầu.

// var creates a property on the global object
var globProp = 'Wisen';

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"

Trong chế độ nghiêm ngặt, giá trị của this sẽ là

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}

const cheese = new Food('feta', 5);
const fun = new Toy('robot', 40);
2.

'use strict';

var globProp = 'Wisen';

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // throws TypeError: Cannot read the property of 'globProp' of undefined

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-function.prototype.call
# sec-function.prototype.call

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Cái nào sau đây được sử dụng để gọi hàm hoặc phương thức trong?

ret function_name () là cách gọi đúng hàm. is the correct way of calling a function.

Cái nào sau đây được sử dụng để gọi hàm trên một phương thức trong Java?

Để gọi một phương thức trong Java, chỉ cần viết tên của phương thức theo sau là hai dấu ngoặc đơn () và dấu chấm phẩy (;).Nếu phương thức có các tham số trong khai báo, các tham số đó được truyền trong dấu ngoặc đơn () nhưng lần này không có kiểu dữ liệu được chỉ định.write the method's name followed by two parentheses () and a semicolon(;). If the method has parameters in the declaration, those parameters are passed within the parentheses () but this time without their datatypes specified.

3 cách để gọi một chức năng trong JS là gì?

Các hàm JavaScript có thể được gọi là:..
Như một chức năng ..
Như một phương pháp ..
Là một người xây dựng ..
qua cuộc gọi và áp dụng ..

Hàm () () trong javascript là gì?

Một hàm trong JavaScript tương tự như một thủ tục, một tập hợp các câu lệnh thực hiện một tác vụ hoặc tính toán một giá trị, nhưng đối với một quy trình để đủ điều kiện làm chức năng, nó sẽ lấy một số đầu vào và trả về một đầu ra trong đó có một số mối quan hệ rõ ràng giữađầu vào và đầu ra.a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

Điều gì sẽ được sử dụng để gọi biểu thức định nghĩa hàm trong JavaScript?

31) Trong JavaScript, những gì sẽ được sử dụng để gọi biểu thức định nghĩa hàm: Nguyên mẫu chức năng.Function prototype.