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

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

问题描述

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



另外,这两个语法的区别是什么:

  var myFirstFunc = function(param){
//做某事
};

  function myFirstFunc(param){
//做某事
};

另外,在使用函数之前,我发现我们需要做这样的事情:

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

为什么需要第一行,它是做什么的?



对不起,如果这些都是基本问题,但我以JS开头,并且感到困惑。

编辑:对于最后一段代码,就是我所说的:

  //这里我们使用this定义我们的方法,在我们引入bob之前b $ b var setAge = function(newAge){
this.age = newAge;
};
//现在我们做bob
var bob = new Object();
bob.age = 30;
//在这里我们只是使用我们已经创建的
的方法bob.setAge = setAge;


解决方案

要回答题目问题,之间的'函数'和'方法'。

它的语义和你试图表达。



在javascript中,每个函数都是一个对象。一个对象是key:value对的集合。如果一个值是一个基元(整数,字符串,布尔值)或另一个对象,则该值被认为是一个属性。如果一个值是一个函数,它被称为'方法'。



在一个对象的范围内,一个函数被称为该对象的一个​​方法。它是从对象命名空间'MyObj.theMethod()'中调用的。既然我们说过一个函数是一个对象,函数中的函数就被认为是该函数的一个方法。你可以说我打算使用我的对象的保存方法。或者你可以说,保存方法接受功能作为参数。但是你通常不会说一个函数接受一个方法作为参数。



顺便说一下,这本书的JavaScript Patterns Stoyan Stefanov 详细地介绍了你的问题,如果你真的想理解语言,我强烈推荐它。这里有一本关于这个主题的书。


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


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
};

and

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?

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

EDIT: 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.

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

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 a function is an object, a function within a function is considered a method of that function. You can say I am going to use the save method of my object. Or you could say, "the save method accepts a function as a parameter". But you generally wouldn't say a function accepts a method as a parameter.

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.

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天全站免登陆