使用函数定义javascript对象 [英] using a function to define javascript objects
问题描述
您好,请问是否 外部的父母是真的有必要吗? 就像这里:
Hello, like to ask if the external parenthises are really necessary? Like here:
incident.Controller =(...) //在此问题的最后看到代码块
incident.Controller = (...) //see code block at the end of this question
我不这么认为。 (至少代码对我来说也是一样的。)
I dont think so. (at least the code works the same for me).
另外,我还想知道下面两段代码之间的区别。
Also, I would also like to know the difference between the two snippet of code below.
在函数中编写以下代码的好处(下面的第一个代码段)是通过使用"use strict"将事物范围扩展到myCompany.incident。 。声明 如下。 ?右  (否则我会把它写在
下面的片段中)
The benefit writing the code below in a function (the first snippet below) is to scope things to myCompany.incident by using the "use strict" statement. Like below. Right? (i would otherwise write it out as in the snippet beneath it)
myCompany.incident =(功能(事件){
" ; use strict"; $
incident.Contracts = {
onLoad:function(){
var controller =&
new myCompany.incident。控制器(Xrm);
controller.load();
}
};
返回事件;
}(myCompany.incident || {}));
myCompany.incident = (function(incident) {
"use strict";
incident.Contracts = {
onLoad: function() {
var controller =
new myCompany.incident.Controller(Xrm);
controller.load();
}
};
return incident;
}(myCompany.incident || {}));
否则我会(如果不是"use strict";)将其写出来:
I would otherwise (if not for "use strict";) write it out like this:
的   myCompany.incident = myCompany.incident || {};
myCompany.incident.Contracts = {onLoad:function(){
      var controller = new myCompany.incident.Controller(Xrm);
    controller.load();
    }
};
我不认为"使用严格;"这是两个片段的唯一区别/后果,这就是这个问题的原因。
I dont think "use strict;" is the only difference/consquence of the two snippet, which is the reason for this question.
谢谢,p。
/// <reference path="controller.js"/>
/*global myCompany: true*/
myCompany = window.myCompany || {};
myCompany.incident = (function(incident) {
"use strict";
incident.Contracts = {
onLoad: function() {
var controller =
new myCompany.incident.Controller(Xrm);
controller.load();
}
};
return incident;
}(myCompany.incident || {}));
谢谢,Paul
推荐答案
包装函数定义通常在直接调用函数时使用paras。查找IIFE(https://en.wikipedia.org/wiki/Immediately-invoked_function_expression)。
Wrapping a function definition in paras is typically used when invoking the function straight way. Lookup IIFE (https://en.wikipedia.org/wiki/Immediately-invoked_function_expression).
var fn = function(arg1){return" result =" + arg1;}
var fn= function(arg1) { return "result="+arg1;}
fn指向未命名(匿名)函数。所以,fn('test')=" result = test"。
fn points to the unnamed (anonymous) function. So, fn('test') = "result=test".
同等。
var fn2 =(function(arg1){return " result =" + arg1;})('another test');
var fn2= (function(arg1) { return "result="+arg1;})('another test');
fn2 =" result = another test"
fn2 = "result=another test"
建议取决于(fn(){})()或(fn(){}())是否最佳。
Advice varies on whether (fn(){})() or (fn(){}()) is best.
有些情况下,使用para并非严格必要但有助于提高可读性 - 理由本身。
There are cases when using paras is not strictly necessary but aids readability - justification in itself.
这篇关于使用函数定义javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!