用JavaScript声明函数 [英] Declaring functions in JavaScript

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

问题描述


可能重复:

Javascript:var functionName = function(){} vs function functionName(){}


这两种声明函数的方式有什么区别?

 函数someFunc(){...} 

var someFunc = function(){...}

我不是在技​​术意义上问。我不是问哪个更适合可读性,或者哪种风格更受欢迎。

解决方案

这里的人们。从技术上讲,这个语法对于声明函数的方式可能是相同的
我在最后一条语句中不正确,我在差异文件中读到了为什么它们在技术上有差异,为什么
;但他们在不断演变的模式中发挥作用的方式非常庞大。我强烈推荐Doughlas Crockford的Javascript:The Good Parts。



但要以微妙而简单的方式证明我的观点;这是一个小例子。

  //现有的全局函数为每个人服务
函数swearOutLoud(swearWord){
alert(You + swearWord);
}
//全局函数的区域在这里结束

//这里是先生。海绵宝宝。他对他的对象非常热情。但他有点粗鲁。
var spongeBob = {
name:squarePants,
swear:function(swearWord){
name =spongy;
alert(你+ swearWord);
返回此;
}
}

//最后spongebob也会学到很好的礼貌。演化!
spongeBob.apologize = function(){
alert(Hey+ this.name +,I'm sorry man!);
返回此;
}


//要求海绵宝宝发誓然后一口气道歉(CASCADING EFFECT !!)
alert(spongeBob.swear(twit))。道歉());

如果您查看上面的代码,我声明了一个名为swearOutLoud的函数。这将需要从任何对象或通话中发誓,并会给你输出。它可以使用传递给它的this参数和参数对任何对象执行操作。

然而,第二个声明被声明为对象的一个​​属性,称为spongeBob 。这一点很重要。因为在这里我正在朝着一个对象驱动的行为迈进。虽然我也保持级联效应,因为我返回this,如果我没有其他东西可以返回。



类似的东西在jquery中完成;如果你试图编写一个框架或者其他东西,这个级联模式就很重要。你也可以将它链接到Builder设计模式。

但是通过将函数声明为对象的属性,我能够实现以对象为中心的行为,从而导致更好的编程范例。除非设计得很好;在全局访问之外声明的单个函数导致非面向对象的编码方式。我不知道如何选择后者。



要看到级联的效果,请看最后一个声明,您可以让spongebob发誓并立即道歉;即使道歉后来被添加为属性。

我希望我明确我的观点。与技术角度的区别可能很小;但从设计和代码演进的角度来看,它是巨大的,使世界有所不同。



但是那只是我!要么接受,要么离开它。 :)



编辑: 所以这两个调用在技术上是不同的;因为命名声明与全局命名空间绑定,并在解析时定义。所以可以在函数声明前调用。

  // success 
swearOutLoud(Damn);

函数swearOutLoud(swearWord){
alert(You+ swearWord)
}

以上代码将正常工作。但是下面的代码不会。

 发誓(该死!); 
var swear = function(swearWord){
console.log(swearWord);
}


Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}

What's the difference between these two ways of declaring a function?

function someFunc() { ... }

var someFunc = function() { ... }

I'm not asking in the technical sense. I'm not asking which is better for readability, or which style is preferred.

解决方案

I am on different opinion with most of the people here. Technically this syntax may mean the same for declaring functions both ways (I stand incorrect on my last statement. I read up on a diff post why they are technically diff and I'll add in the end, why) ; but the way they play a role in evolving patterns is massive. I would highly recommend "Javascript: The Good Parts" by Doughlas Crockford.

But to prove my point in a subtle and a simple manner; here is a small example.

//Global function existing to serve everyone
function swearOutLoud(swearWord) {
    alert("You "+ swearWord);           
}
//global functions' territory ends here

//here is mr. spongebob. He is very passionate about his objects; but he's a bit rude.
var spongeBob = {
    name : "squarePants",
    swear : function(swearWord) {
                name = "spongy";
                alert("You "+ swearWord);
                return this;
            }
}

//finally spongebob learns good manners too. EVOLUTION!
spongeBob.apologize = function() {
    alert("Hey " + this.name + ", I'm sorry man!");
    return this;
}


//Ask spongebob to swear and then apologize in one go (CASCADING EFFECT!!)
alert(spongeBob.swear("twit").apologize());

if you look at the code above I declared a function with a name swearOutLoud. Which would take a swear word from any object or a call and will give you the output. It can do operations on any object using the "this" parameter that is passed to it and the arguments.

However second declaration is declared as an attribute of object called "spongeBob". This is important to note; as here I am moving towards an object driven behavior. While I am also maintaining "cascading effect" as I return "this" if i have nothing else to return.

Somthing similar is done in jquery; and this cascading pattern is important if you are trying to write a framework or something. You'll link it to Builder design pattern also.

But with functions declared as an attributes of an object I am able to achieve an object centric behavior which leads to a better programming paradigm. Unless designed well; individual functions declared outside with global access lead to a non-object oriented way of coding. I somehow prefer the latter.

To see cascading in effect, look at the last statement where you can ask spongebob to swear and apologize at once; even though apologize was added as an attribute later on.

I hope I make my point clear. The difference from a technical perspective may be small; but from design and code evolution perspective it's huge and makes a world of a difference.

But thats just me! Take it or leave it. :)

EDIT:

So both the calls are technically different; because a named declaration is tied to global namespace and is defined at parse time. So can be called even before the function is declared.

 //success
 swearOutLoud("Damn");

function swearOutLoud(swearWord) {
    alert("You " + swearWord)
}

Above code will work properly. But code below will not.

swear("Damn!");
    var swear = function(swearWord) {
    console.log(swearWord);
}

这篇关于用JavaScript声明函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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