Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this code. It creates an object with x and y field. I want to add a method, which creates new object with additional width and height fields. But despite my tryings it keeps returning undefined. What is wrong?

JSFiddle

function $ (x, y) {
    this.x = x;
    this.y = y;

    return this;
}

$.prototype.$ = function (x, y) {
    this.width = x - this.x;
    this.height = y - this.y;

    return this;
}

var a = $(10,10).$(30,30);
alert(a.width);
share|improve this question
    
When you mention the word "it", to which part of the code are you referring? :) –  summea Apr 26 '13 at 17:41
    
I could not find the correct word (I'm not so good in english, sorry :P) –  Are Wojciechowski Apr 26 '13 at 17:43
    
you show the result of a.width. what about a? is it also undefined? –  Daniel S. Apr 26 '13 at 17:44
    
No problem; just trying to clarify... because "it" could be any part of your code ;) –  summea Apr 26 '13 at 17:45
    
@DanielS. a is defined as an Object. it works now :) –  Are Wojciechowski Apr 26 '13 at 17:46

2 Answers 2

up vote 3 down vote accepted

You missed the new before $; This works:

var a = new $(10,10).$(30,30);
share|improve this answer
    
thanks, silly me. –  Are Wojciechowski Apr 26 '13 at 17:43
var a = (new $(10,10)).$(30,30); //You need new
alert(a.width);

Also it might not be a good idea to have an instance function of a class to have the same name as the class -- it is a little confusing.


Here is how you can do what you want to do with 2 "Point" objects (as asked for in the comments):

var Point = (function(){
    var Point = function(x, y) {
        this.width = x;
        this.height = y;
    }
    Point.prototype.removePoint = function(point) {
        return new Point(point.width - this.width, point.height - this.height);
    }

    return Point;
})()

var a = new Point(10,10);
var b = new Point(30,30);

var c = a.removePoint(b);
alert(c.width);

Fiddle: http://jsfiddle.net/maniator/d3fx7/

share|improve this answer
    
It's not when you think about $ as a points. –  Are Wojciechowski Apr 26 '13 at 17:44
    
@AreWojciechowski it is redundant and confusing. It might be a good idea to give actual names to functions :-) –  Neal Apr 26 '13 at 17:45
    
If we are talking about naming now, as well, you may want to avoid using $ for a function (or object) if you start using certain JavaScript libraries out there, as well. –  summea Apr 26 '13 at 17:46
    
@summea a few libraries use $, not only jQuery :-P –  Neal Apr 26 '13 at 17:47
    
@Neal And that is why I wrote the plural form of "library"... ;) (and now the links are updated) –  summea Apr 26 '13 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.