[英]Differences in javascript module pattern
Very simple question, not sure if there are any differences in these ways of creating a javascript "module". 非常简单的问题,不确定在创建javascript“模块”的这些方式上是否存在任何差异。 I'm hoping somebody can clarify it for me.
我希望有人可以为我澄清一下。
A) 一种)
var foo = function() {
var bar = function() {
console.log('test');
};
return {
bar: bar
};
};
B) B)
var foo = function() {
function bar() {
console.log('test');
};
return {
bar: bar
};
};
C) C)
var foo = function() {
this.bar = function() {
console.log('test');
};
return {
bar: this.bar
};
};
A and B are essentially the same, though there is a very minor difference between A and B due to function/variable hoisting, theoretically you could write code which would work in B but break in A, but practically speaking you'd have to really write weird code to do so. A和B本质上是相同的,尽管由于功能/变量提升,A和B之间的差别很小,但从理论上讲,您可以编写可以在B中工作但会破坏A的代码,但实际上您必须编写奇怪的代码来做到这一点。
C will work, but is conceptually wrong. C可以工作,但是从概念上讲是错误的。 The point of using
this.funcName
in a function is as a constructor (creating lots of objects using new Thing()
. If you aren't using the function as a constructor you shouldn't be using that style as someone scanning the code may mistake the function as a constructor instead of its actual purpose which is a module. 在函数中使用
this.funcName
的要点是作为构造函数(使用new Thing()
创建许多对象。如果不将函数用作构造函数,则不应使用该样式,因为有人在扫描代码可能会将函数误认为是构造函数,而不是将其视为模块的实际用途。
At first, you forgot to execute the function expression: the module pattern is an IEFE. 首先,您忘记执行函数表达式: 模块模式是IEFE。 You just create a function.
您只需创建一个函数。
Your last example is nonsense, it looks like a constructor function when assigning properties to this
- and when executed as a IEFE it breaks (and using it with new
has undesired effects; an when returning an object it's useless). 你的最后一个例子是无稽之谈,分配属性时,它看起来像一个构造函数
this
(和使用它与当作为IEFE执行它打破- new
;返回一个对象也没用时已经不期望的效果)。
For the difference between the first and the second snippet see var functionName = function() {} vs function functionName() {} . 有关第一个代码段和第二个代码段的区别,请参见var functionName = function(){}与function functionName(){} 。 In context of the module pattern, the function declaration is recommended.
在模块模式的上下文中,建议使用函数声明。
//Javascript Module Pattern var person = (function() { var cname = 'CheapFlight'; return { name: "Santosh Thakur", getAge: function() { return cname; }, growOlder: function() { return cname + " Updated"; } }; }()); person.cname = "New Company" console.log(person.cname); console.log(person.name); console.log(person.getAge()); console.log(person.growOlder());
prefix var before a function makes it a "class"-ish, this means you can make many versions of it. 在函数使它成为“类”之前添加var前缀,这意味着您可以创建它的许多版本。 This goes for A
这适合A
For example: 例如:
var hi = function()
{
var bye = function()
{
alert("bye");
}
bye(); // this will call bye
var something = new bye(); // this will create a new instance of bye();
}
var something = new hi();
something();
B means you can only call bar, not make a new instance of it inside the function. B表示您只能调用bar,而不能在函数中创建它的新实例。
C is the same as bar because of its scope C的范围与Bar相同
Class-ish: 类:
var Dog = function( hair, type )
{
this.hair = hair;
this.type = type;
}
var fred = new Dog( "long", "Dalmation" );
alert( fred.hair );
var dave = new Dog( "short", "Poodle" );
alert( dave.type);
This is a class ^ 这是一堂课^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.