This question already has an answer here: What is the 'new' keyword in JavaScript? 13 answers methodA is not a method, because the new operator causes the function after it to be called as a constructor. So you get back an object for methodA with the anonymous function as the equivalent of its class. It's as if you had written this: var MethodA = function() { alert('a');
这个问题在这里已经有了答案: JavaScript中的'new'关键字是什么? 13个答案 methodA不是一个方法,因为new操作符会在它作为构造函数被调用之后导致该函数。 因此,您可以使用匿名函数返回methodA的对象作为其类的等价物。 这就好像你写了这样的: var MethodA = function() { alert('a'); }; this.methodA = new MethodA; 最后一行与此相同: this.methodA = new MethodA(); 新功能用于创建对象。 这
This question already has an answer here: What's the difference between Array(1) and new Array(1) in JavaScript? 3 answers What is the 'new' keyword in JavaScript? 13 answers The behaviour of a native method depends on the EcmaScript specification. For Date the spec says : When Date is called as a function rather than as a constructor, it returns a String representing the
这个问题在这里已经有了答案: Array(1)和JavaScript中的新Array(1)有什么区别? 3个答案 JavaScript中的'new'关键字是什么? 13个答案 本地方法的行为取决于EcmaScript规范。 对于Date规格说: 当Date被作为函数而不是构造函数调用时,它会返回一个表示当前时间(UTC)的String。 注:函数调用Date(...)与具有相同参数的对象创建表达式new Date(...) 不等效 。 规范说,对Array 当数组被调用
Possible Duplicate: What is the 'new' keyword in JavaScript? I'm learning about prototypes in Javascript and wondered what this code is doing. It's not like what I've run across in Java or C#: function MyObject(Parameter) { this.testString = Parameter; } var objectRef = new MyObject( "myValue" ); What's going on with that new MyObject("value")
可能重复: JavaScript中的'new'关键字是什么? 我正在学习Javascript中的原型,并想知道这段代码在做什么。 这不像我在Java或C#中运行的那样: function MyObject(Parameter) { this.testString = Parameter; } var objectRef = new MyObject( "myValue" ); 新的MyObject(“value”)位发生了什么? 我明白,在JavaScript函数中是对象,但是我仍然围绕着当你new()函数发生了什么事情的头脑?
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure: should i use the “new” keyword? See this code: function friend(name) { return { name: name }; } var f1 = friend('aa'); var f2 = new friend('aa'); alert(f1.name); // -> 'aa' alert(f2.name); // -> 'aa' What's the difference between f1 and f2 ? The new in your case isn&
可能重复: JavaScript中的'new'关键字是什么? 从JS关闭创建对象:我应该使用“新”关键字? 看到这个代码: function friend(name) { return { name: name }; } var f1 = friend('aa'); var f2 = new friend('aa'); alert(f1.name); // -> 'aa' alert(f2.name); // -> 'aa' f1和f2什么区别? 你的情况是新的不是有用的。 当函数使用'this'关键字时,您只需使用new关键字。 function f(
This question already has an answer here: What is the 'new' keyword in JavaScript? 13 answers When we call a function with the new keyword. the following will happen: A new object will be created in the memory The scope of that object will be passed to the function; So the this keyword will refer to that object. The newly created object will be returned. So in essence, that
这个问题在这里已经有了答案: JavaScript中的'new'关键字是什么? 13个答案 当我们用new关键字调用函数时。 会发生以下情况: 内存中将创建一个new对象 该对象的范围将传递给该函数; 所以这个关键字将引用该对象。 新创建的对象将被返回。 实质上,这就是您如何使用JavaScript创建实例。 您需要使用new关键字调用函数。 这样做时,该函数被称为构造函数。 在你的例子中, q函数返回doStuff方法的一
This question already has an answer here: What is the 'new' keyword in JavaScript? 13 answers In your particular instance, No, there is no difference. Eitherw way, your function will return a self defined Object. By invoking a function with the new keyword, ECMAscript will automatically create a new object for you (alongside doing some magic with prototype and constructor properti
这个问题在这里已经有了答案: JavaScript中的'new'关键字是什么? 13个答案 在你的特定情况下,不,没有区别。 无论如何,你的函数将返回一个自定义的对象。 通过用new关键字调用一个函数,ECMAscript会自动为你创建一个新对象(除了做一些prototype和constructor属性的魔术外),你可以通过this在function (-constructor)访问/写入。 再一次,你的return { }调用该函数,将始终返回该对象引用。
I just watched Douglas Crockford talk about how prototypical inheritance is "not a good idea either" YouTube 35m55s I don't really care about his views on Prototypical inheritance in conjunction with JavaScript since it is such an essential part of the language that it will always be there. But I would like to know what benefits I am reaping by using the functional object crea
我刚看到道格拉斯克罗克福德谈论原型继承如何“不是一个好主意” YouTube 35m55s 我并不真正关心他对JavaScript中与原型继承有关的观点,因为它是语言的重要组成部分,所以它始终存在。 但我想通过使用他在链接中显示的功能对象创建来获得我收获的好处: // Class Free Object Oriented Programming function constructior(init) { var that = other_constructor(init), member, method = function () {
I've been reading "Javascript: The Good Parts" by Douglas Crockford - and while it's a bit extreme, I'm on board with a lot of what he has to say. In chapter 3, he discusses objects and at one point lays out a pattern (also found here) for simplifying & avoiding some of the confusion/issues that come with the use of the built-in "new" keyword. if (typeof Obj
我一直在阅读道格拉斯克罗克福德的“Javascript:The Good Parts” - 尽管有点极端,但我有许多他必须说的话。 在第3章中,他讨论了对象,并在一点上列出了一种模式(也在此处找到),以简化和避免使用内置“新”关键字带来的一些混淆/问题。 if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } newObject = Obj
I watched a talk by Douglas Crockford on the good parts in Javascript and my eyes were opened. At one point he said, something like, "Javascript is the only language where good programmers believe they can use it effectively, without learning it." Then I realized, I am that guy. In that talk, he made some statements that for me, were pretty surprising and insightful. For example, J
我观看了道格拉斯克罗克福德关于Javascript中的优秀部分的演讲,并且我的眼睛被打开了。 他说,有一点像“Javascript是唯一的语言,好的程序员相信他们可以有效地使用它,而不需要学习它。” 然后我意识到,我就是那个人。 在那次演讲中,他发表了一些对我来说非常令人惊讶和有见地的发言。 例如,JavaScript是这个星球上最重要的编程语言。 或者它是这个星球上最流行的语言。 而且,它以许多严肃的方式被打破。 他对我所
Alan Storm's comments in response to my answer regarding the with statement got me thinking. I've seldom found a reason to use this particular language feature, and had never given much thought to how it might cause trouble. Now, I'm curious as to how I might make effective use of with , while avoiding its pitfalls. Where have you found the with statement useful? Another use occ
艾伦风暴的响应我的回答有关意见with说法让我思考。 我很少找到使用这种特定语言功能的理由,也从未考虑过它会如何引起麻烦。 现在,我很好奇,我怎么可能有效利用with ,同时避免其陷阱。 你在哪里发现with语句有用? 今天我发现了另一种用法,所以我兴奋地搜索了网页,并发现了一个现有的内容:在块范围内定义变量。 背景 尽管JavaScript与C和C ++表面相似,但它并没有将变量的范围限制在它们所定义的块中: var nam