gpt4 book ai didi

javascript - 为什么要传递未定义的 javascript 函数参数?

转载 作者:行者123 更新时间:2023-11-30 15:10:00 25 4
gpt4 key购买 nike

所以我正在学习 Javascript,我看到了这段代码:

var apple = {//... an object with some properties};
var fruit = apple.someMethod(function (b) {return b.a_property_of_apple});

其中 someMethod 和 a_property_of_apple 是有效的方法和属性。

我的问题与匿名函数的参数 b 有关,该函数未在其他任何地方声明或定义:

function (b) {return ...

这是怎么回事?什么是 b,为什么要使用它?

提前为问题的基本性质道歉。如果有人只是想把一些重点术语放在我身上以便继续阅读,那就太缺乏解释了。

最佳答案

匿名函数是一个callback function被传递给 apple.method()调用。

apple.method()将在执行期间的某个时刻调用该匿名函数(或将其传递给另一个函数)。每当它被调用时,都会使用回调中可用的参数来调用它。你可以称它为b , 或 response ,或任何你想要的(逻辑名称最好)并能够在匿名函数中使用它。

你应该阅读 Callback functions在 MDN 上。

编辑:我会向你解释这些部分

var apple = {}这是一个对象的定义

var fruit = apple.someMethod(function (b) {return b.a_property_of_apple});定义 fruit 等于调用 apple.someMethod(...) 的返回值

apple.someMethod(function (b) {return b.a_property_of_apple});是对 apple.someMethod 的调用与 function (b) {return b.a_property_of_apple}作为唯一的论点。

b匿名函数中的参数 function (b) {return b.a_property_of_apple}将传递给它在 apple.someMethod 中的调用.

这是一个示例片段。

// define apple
var apple = {
// define method
someMethod: function( callback ) {
var obj = {
a_property_of_apple: "Eat me!" // this will be returned
}

// return the invocation of callback with obj as argument
return callback(obj);
}
}

var fruit = apple.someMethod(function (b) {return b.a_property_of_apple});

console.log(fruit);

编辑:好的,将使用稍微不那么抽象的东西作为示例。

// notice employees being passed to this function
// that is called an argument and is usable inside the function
var orginization = function( employees ) {
// this will take the empoyees argument and assign it to this.employees
// or set this.employees to an empty array if there is no employees argument
this.employees = employees || [ ];

// this is a method ( a method is a function on an object )
// this function takes 3 arguments
this.addEmployee = function( employee ) {
// we use the 3 arguments to push a new object with title, name, and salary
// properties provided by the function arguments
this.employees.push( employee );
}

// this method returns the value stored in this.employees
this.getEmployees = function() {
return this.employees;
}
}

// this is a variable an array of employees only containing 1 employee
// i will use it in the creation of my new orginization
var employess = [
{
title: "CEO",
name: "Enola",
salary: "$$$$$$$"
}
];


// i use the new to create learningInc from originization( employees )
// originization is a constructor function which creates an object
// with methods and properties found on the constructor
var learningInc = new orginization( employess );


// console.log learningInc.getEmployees() an you will see still only the CEO
// works here

console.log( "before newHire: ", learningInc.getEmployees() );

// lets make a newHire
var newHire = {
title: "Peon",
name: "Sadly McFrownFace",
salary: "$"
};

// add the newHire to the employess of learningInc wth out getEmployees() method
learningInc.addEmployee( newHire );


// log the new value of learningInc.getEmployees and you see we now have 2 employees
console.log( "after newHire: ", learningInc.getEmployees() );

好的,现在注意这行 var learningInc = new orginization( employess );

我作为参数传递给此函数的 employees 变量在此函数中使用 var orginization = function( employees ) { ... } .

希望这对您有所帮助。

关于javascript - 为什么要传递未定义的 javascript 函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45247302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com