因此,我正在学习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()将在执行过程中的某个时候调用该匿名函数(或将其传递给另一个函数)。每当调用它时,都将使用在回调内部可用的参数来调用它。您可以将其称为bresponse,或任何您想要的名称(逻辑名称最好),并能够在匿名函数中使用它。

您应该在MDN上阅读有关Callback functions的信息。

编辑:我将向您解释零件

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

var fruit = apple.someMethod(function (b) {return b.a_property_of_apple});定义水果等于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 );

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

希望能有所帮助。

09-25 18:13
查看更多