方法与函数,以及其他问题 [英] Method vs Functions, and other questions

查看:34
本文介绍了方法与函数,以及其他问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于JS,两者有什么区别?我知道方法与对象相关联,但我很困惑函数的目的是什么?它们各自的语法有何不同?

With respect to JS, what's the difference between the two? I know methods are associated with objects, but am confused what's the purpose of functions? How does the syntax of each of them differ?

另外,这两种语法有什么区别:

Also, what's the difference between these 2 syntax'es:

var myFirstFunc = function(param) {
    //Do something
};

function myFirstFunc(param) {
    //Do something
};

另外,我在某处看到我们需要在使用函数之前做这样的事情:

Also, I saw somewhere that we need to do something like this before using a function:

obj.myFirstFunc = myFirstFunc;
obj.myFirstFunc("param");

为什么需要第一行,它有什么作用?

Why is the first line required, and what does it do?

抱歉,如果这些是基本问题,但我是从 JS 开始的,我很困惑.

Sorry if these are basic questions, but I'm starting with JS and am confused.

对于最后一点代码,这就是我要说的:

For the last bit of code, this is what I'm talking about:

// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
  this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
// and down here we just use the method we already made
bob.setAge = setAge;

推荐答案

回答关于函数"和方法"之间有什么区别的标题问题.

To answer your title question as to what is the difference between a 'function' and a 'method'.

这是语义,与您要表达的内容有关.

It's semantics and has to do with what you are trying to express.

在 JavaScript 中,每个函数都是一个对象.对象是键值对的集合.如果值是原始值(数字、字符串、布尔值)或另一个对象,则该值被视为属性.如果一个值是一个函数,它被称为一个方法".

In JavaScript every function is an object. An object is a collection of key:value pairs. If a value is a primitive (number, string, boolean), or another object, the value is considered a property. If a value is a function, it is called a 'method'.

在对象的范围内,函数被称为该对象的方法.它是从对象命名空间 MyObj.theMethod() 调用的. 既然我们说函数是一个对象,那么函数内的函数可以被认为是该函数的方法.

Within the scope of an object, a function is referred to as a method of that object. It is invoked from the object namespace MyObj.theMethod(). Since we said that a function is an object, a function within a function can be considered a method of that function.

您可以这样说:我将使用对象的保存方法."并且这个保存方法接受一个函数作为参数."但是你通常不会说一个函数接受一个方法作为参数.

You could say things like "I am going to use the save method of my object." And "This save method accepts a function as a parameter." But you generally wouldn't say that a function accepts a method as a parameter.

顺便说一句,Stoyan 的JavaScript Patterns一书Stefanov 详细介绍了您的问题,如果您真的想了解该语言,我强烈推荐它.这是书中关于这个主题的引述:

Btw, the book JavaScript Patterns by Stoyan Stefanov covers your questions in detail, and I highly recommend it if you really want to understand the language. Here's a quote from the book on this subject:

因此,作为对象的函数 A 可能具有属性和方法,其中一个恰好是另一个函数 B.然后 B 可以接受函数 C 作为参数,并且在执行时可以返回另一个函数D.

So it could happen that a function A, being an object, has properties and methods, one of which happens to be another function B. Then B can accept a function C as an argument and, when executed, can return another function D.

这篇关于方法与函数,以及其他问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆