Whats the difference between defining the method "area" as a property of "this" instead of "prototype"?  //console.clear()  function Rectangle(w, h)  {     this.width = w;     this.height = h;     this.area = function( ) { return this.width * this.height; } }   var r = new Rectangle(2, 3); var a = r.area( );  //console.log(a)  function Square(s)  {     this.side= s; }  Square.prototype.area = function(){return this.side * this.side; }  var r = new Square(2); var a = r.area( );  //console.log(a)   In JavaScript - The definitive guide in the section Prototypes and Inheritance of Chapter 9 , part 1, the author says that defining the method "area" inside the prototype object is beneficial, but his explanation wasn't really understandable:     "..the area of every single Rectangle   object always refers to the same   function (someone might change it, of   course, but you usually intend the   methods of an object to be constant).   It is inefficient to use regular   properties for methods that are   intended to be shared by all objects   of the same class (that is, all   objects created with the same   constructor)."   I know this question almost looks like  one, but it is not.
3978
What's the difference between  var A = function () {     this.x = function () {         //do something     }; };   and  var A = function () { }; A.prototype.x = function () {     //do something };
44431
Could you explain the difference between setting methods in the constructor and through prototype object? The following code shows these two ways of setting the methods - say_hello and say_bye both work fine:  function MessageClass() {   this.say_bye = function() { alert('see ya'); }; }  MessageClass.prototype.say_hello = function() { alert('hello'); };  x = new MessageClass(); x.say_hello(); x.say_bye();
330
The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself).  Although it doesn't really matter at the end, I want to know what is the correct way to present the arguments passing conventions. Is there an excerpt from JavaScript specification, which defines what should be the semantics regarding this?
71027
The following code fails to compile stating  "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else":           var l = new List&lt;string&gt;();         l.Find(st =&gt; st.EndsWith("12"));         string st = "why this fails?";   I understand why this won't work:          string preParent = "";         {             string preParent = "Should fail cause we change the meaning";         }   When we do the following we get "CS0103: The name 'postParent' does not exist in the current context":          {             string postParent=string.Empty;         }         postParent = "Should this work?";   What I don't get is why is the compiler smart enough to see that postParent is not within scope, but won't let me define a new variable that has the same name as a variable used within a child scope (which is obviously out of scope at this point).   Is the compiler simple enforcing scope by refusing to let me use the variable? If so this makes sense.  ===========  Edited:   I guess what I also find interesting is how you can have the same variable within two child scopes in a single method, so this is valid:          {             string thisWorks= string.Empty;         }         {             string thisWorks= "Should this work?";         }   I'm just a little curious that you can have two variables with the same name as long as they are at the same level (if you look at scope as a tree). This makes sense because you can have local variables in two methods of the same class with the same name.   I'm just surprised that the compiler is able to differentiate and allow this, while it wouldn't allow the postParent variable. And is this a technical limitation or was this a design decision? That's what I'm really trying to get at;-)
15653
Possible Duplicate:          I'm looking to make a callable JavaScript object, with an arbitrary prototype chain, but without modifying Function.prototype.  In other words, this has to work:  var o = { x: 5 }; var foo = bar(o); assert(foo() === "Hello World!"); delete foo.x; assert(foo.x === 5);   Without making any globally changes.
7437
I have a method that uses the  function and makes a call to another method. On initial load method 2 works fine. However, after the timeout, I get an error that says method2 is undefined. What am I doing wrong here?  ex:  test.prototype.method = function() {     //method2 returns image based on the id passed     this.method2('useSomeElement').src = "http://www.some.url";     timeDelay = window.setTimeout(this.method, 5000); };  test.prototype.method2 = function(name) {     for (var i = 0; i &lt; document.images.length; i++) {         if (document.images[i].id.indexOf(name) &gt; 1) {             return document.images[i];         }     } };
115518
What's the term for this design?  object.method1().method2().method3()   ..when all methods return *this?  I found the term for this a while ago, but lost it meanwhile. I have no clue how to search for this on google :) Also if anyone can think of a better title for the question, feel free to change it.  Thanks  Update-Gishu: After reading about it, I feel that your question is misleading w.r.t. code snippet provided.. (Feel free to rollback)  Method Chaining  object.method1().method2().method3()   Fluent Interfaces  private void makeFluent(Customer customer) {         customer.newOrder()                 .with(6, "TAL")                 .with(5, "HPK").skippable()                 .with(3, "LGV")                 .priorityRush();     }
69276
Learning Objective-C and reading sample code, I notice that objects are usually created using this method:  SomeObject *myObject = [[SomeObject alloc] init];   instead of:  SomeObject *myObject = [SomeObject new];   Is there a reason for this, as I have read that they are equivalent?
34146
This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations:  string hello = "hello";   vs.   string hello_alias = @"hello";   Printing out on the console makes no difference, the length properties are the same.
18490
Can someone provide a simple explanation of methods vs. functions in OOP context?
62479
I am doing , and am having a few problems with function notation. I understand the form f(x).  What does the form f: x ↦ y mean? Could you also give one or two examples?  And, if possible, state your source. Thank you.
12181
Is there a best-practice or common way in JavaScript to have class members as event handlers?  Consider the following simple example:  &lt;head&gt;     &lt;script language="javascript" type="text/javascript"&gt;          ClickCounter = function(buttonId) {             this._clickCount = 0;             document.getElementById(buttonId).onclick = this.buttonClicked;         }          ClickCounter.prototype = {             buttonClicked: function() {                 this._clickCount++;                 alert('the button was clicked ' + this._clickCount + ' times');             }         }      &lt;/script&gt; &lt;/head&gt; &lt;body&gt;     &lt;input type="button" id="btn1" value="Click me" /&gt;     &lt;script language="javascript" type="text/javascript"&gt;         var btn1counter = new ClickCounter('btn1');     &lt;/script&gt; &lt;/body&gt;   The event handler buttonClicked gets called, but the _clickCount member is inaccessible, or this points to some other object.  Any good tips/articles/resources about this kind of problems?
38893
I prefer to use OOP in large scale projects like the one I'm working on right now. I need to create several classes in JavaScript but, if I'm not mistaken, there are at least a couple of ways to go about doing that. What would be the syntax and why would it be done in that way?  I would like to avoid using third-party libraries - at least at first. Looking for other answers, I found the article  that discusses object-oriented programming in JavaScript. Is there a better way to do inheritance?
22304
Hi All: I have a code snippet below which works beautifully and was provided by someone on this list to me a while back.  But now I want to change it so that I draw one long rectangle instead of the adjacent five boxes. The text inside the rectangle should just be at the far right of it and could say $Y^{*}_{whatever}$. I'm thinking that I need to define a rectnode instead of a squared node with different things in it.  But I don't understand how to make it the same length as the length of the boxes and in the same position. &quot;minimum size&quot; currently inside the definition of squarednode isn't making sense to me. EDIT: I have edited this question by including a handwritten drawing in order to express the fact that I need the rectangles to line up with the horizontal axis as shown. So, the first rectangle travels from 0 to 5. The second rectangle travels from 1 to 6, etc all the way to the 6th rectangle travelling from 5 to 10. Also, all the rectangles should be the same width and length even though it may not seem that way by the picture. I also don't need a y-axis. Thanks and I'm sorry for not being clearer the first time.  \begin{tikzpicture}[ squarednode/.style={draw=black, minimum size=9.9mm}, y=1.5cm ]    %boxes      \draw (0.5, 5.5) node[squarednode] {$X_{1}^{*}$};     \draw (1.5, 5.5) node[squarednode] {$X_{2}^{*}$};     \draw (2.5, 5.5) node[squarednode] {$X_{3}^{*}$};     \draw (3.5, 5.5) node[squarednode] {$X_{4}^{*}$};     \draw (4.5, 5.5) node[squarednode] {$X_{5}^{*}$};                  \draw (1.5, 4.5) node[squarednode] {$X_{2}^{*}$};     \draw (2.5, 4.5) node[squarednode] {$X_{3}^{*}$};     \draw (3.5, 4.5) node[squarednode] {$X_{4}^{*}$};     \draw (4.5, 4.5) node[squarednode] {$X_{5}^{*}$};     \draw (5.5, 4.5) node[squarednode,dashed] {0};      \draw (2.5, 3.5) node[squarednode] {$X_{3}^{*}$};     \draw (3.5, 3.5) node[squarednode] {$X_{4}^{*}$};     \draw (4.5, 3.5) node[squarednode] {$X_{5}^{*}$};     \draw (5.5, 3.5) node[squarednode,dashed] {0};     \draw (6.5, 3.5) node[squarednode,dashed] {0};          \draw (3.5, 2.5) node[squarednode] {$X_{4}^{*}$};     \draw (4.5, 2.5) node[squarednode] {$X_{5}^{*}$};     \draw (5.5, 2.5) node[squarednode,dashed] {0};     \draw (6.5, 2.5) node[squarednode,dashed] {0};     \draw (7.5, 2.5) node[squarednode,dashed] {0};          \draw (4.5, 1.5) node[squarednode] {$X_{5}^{*}$};     \draw (5.5, 1.5) node[squarednode,dashed] {0};     \draw (6.5, 1.5) node[squarednode,dashed] {0};     \draw (7.5, 1.5) node[squarednode,dashed] {0};     \draw (8.5, 1.5) node[squarednode,dashed] {0};          \draw (5.5, 0.5) node[squarednode,dashed] {0};     \draw (6.5, 0.5) node[squarednode,dashed] {0};     \draw (7.5, 0.5) node[squarednode,dashed] {0};     \draw (8.5, 0.5) node[squarednode,dashed] {0};     \draw (9.5, 0.5) node[squarednode,dashed] {0};                      \end{tikzpicture}
112799
In JavaScript, there are two values which basically say 'I don't exist' - undefined and null.  A property to which a programmer has not assigned anything will be undefined, but in order for a property to become null, null must be explicitly assigned to it.  I once thought that there was a need for null because undefined is a primitive value and null an object. It's not, even if typeof null will yield 'object': Actually, both are primitive values - which means neither undefined nor null can be returned from a constructor function, as both will be converted to an empty object (one has to throw an error to proclaim failure in constructors).  They also both evaluate to false in boolean contexts. The only real difference I can think of is that one evaluates to NaN, the other to 0 in numeric contexts.  So why is there both undefined and null if this just confuses programmers who are incorrectly checking for null when trying to find out whether a property has been set or not?  What I'd like to know is if anyone has a reasonable example where it's necessary to use null which couldn't be expressed using undefined instead.  So the general consensus seems to be that undefined means 'there is no such property' while null means 'the property does exist, but holds no value'.  I could live with that if JavaScript implementations would actually enforce this behavior - but undefined is a perfectly valid primitive value, so it can easily be assigned to existing properties to break this contract. Therefore, if you want to make sure if a property exists, you have to use the in operator or hasOwnProperty() anyway. So once again: what's the practical use for separate values for undefined and null?  I actually use undefined when I want to unset the values of properties no longer in use but which I don't want to delete. Should I use null instead?
107998
Is it like...  var obj = new Object();  obj.function1 = function(){     //code }   or something like that?
14258
I wonder if there is any difference between mapping and a function. Somebody told me that the only difference is that mapping can be from any set to any set, but function must be from $\mathbb R$ to $\mathbb R$. But I am not ok with this answer. I need a simple way to explain the differences between mapping and function to a lay man together with some illustration (if possible).  Thanks for any help.