SitePoint Sponsor

User Tag List

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 94
  1. #51
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Correction: everything except literal numbers are objects in JS. So there's no way to add 1.add(2) functionality to the language, except to do something like (new Number(1)).add(2).

    EDIT: "var x = 1; alert(x.add(2));" also works
    Can you do operator overloading in Javascript?

    Another Ruby example:

    Code:
    class Putty
      attr_reader :weight
    
      def initialize(weight = 10)
        @weight = weight
      end
    
      def + (more_putty)
        Putty.new(@weight + more_putty.weight)
      end
    
    end
    
    first_blob = Putty.new  #=> @weight = 10
    second_blob = Putty.new #=> @weight = 10
    
    big_blob = first_blob + second_blob 
    
    big_blob #=> @weight = 20
    Sure you could use .add() to get the same effect, or you could use functions and an associative array instead if you like, all the same result.

    Though a + b looks nicer than a.add(b) or add(a, b) or (add a b) imo

    Douglas
    Hello World

  2. #52
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, no operator overloading in JS. Dunno about JS2, but I wouldn't expect it to be in there either since operator overloading is considered messy by many and "feature bloat" for the language (I disagree with this BTW).

    Only numeric and string types support arithmetic operators (1 + 1 => 2; 'a' + 'b' => 'ab'), and the rest will try to autocast to either string or number ('today is ' + objDate).

    Also, there's actually no add function for numbers. In fact, it's totally unnecessary since 1 + 2 works just as well. I just made that method up as an example, though the method could be dynamically added:

    Code:
    Number.prototype.add = function(x) {
      return this.valueOf() + x;
    }
    
    var x = 1;
    x = x.add(2);
    I was just pointing out that in JS, number literals (e.g. 1) aren't automatically converted to the Number object unless assigned to a variable, so 1.some_method() would never work.

  3. #53
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Code:
    Number.prototype.add = function(x) {
      return this.valueOf() + x;
    }
    
    var x = 1;
    x = x.add(2);
    That's quite nice, didn't know you could do that in JS
    Hello World

  4. #54
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Nope, no operator overloading in JS. Dunno about JS2, but I wouldn't expect it to be in there either since operator overloading is considered messy by many and "feature bloat" for the language (I disagree with this BTW).
    One of the things I find most interesting about ruby is that most operators are actually syntactic sugar for method calls to the relevant objects; e.g. a*b+c actually translates to (a.*(b)).+(c). This makes operator overloading quite intuitive.

  5. #55
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Operator overloading is a tricky one for me. I think in web apps there is little need for it and it's all too easy for a novice to abuse this feature. Something like C++, it is more suited as things like matrice/vector classes, smart pointers etc make good use of them. Still, I guess it's a useful feature

  6. #56
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    Javascript is my favorite as well
    Here's an interesting page on closures in Javascript: http://jibbering.com/faq/faq_notes/closures.html

    Quote Originally Posted by MiiJaySung
    Operator overloading is a tricky one for me. I think in web apps there is little need for it and it's all too easy for a novice to abuse this feature.
    The biggest problem I've seen is confusion about what the operators mean. In Ruby, you might see someone new to the language using + instead of << to add items to a collection. In the "skeleton" thread, which is the "chain of responsibility" thread at the moment, I was paying with the ideas in Ruby, using code like this:

    Code:
    chain << handler1 << handler2
    Which is a quite nice use of operators imo. (chain extends from Array here)

    There does seem to be a tendancy to have an "if someone who I think is a poorer programmer than me is working on code I'll have to use, I'd rather it was statically typed" opinion around. Which Ruby doesn't fit very well:

    Code:
    class Person
      def talk
        puts "hello"
      end
    end
    
    jack = Person.new
    jill = Person.new
    
    class << jack # which doesn't mean the same as << above!
      def talk
        puts "Hello! I'm Jack!"
      end
    end
    
    jack.talk #=> Hello! I'm Jack!
    jill.talk #=> hello
    the concept of types == classes really doesn't work in very dynamic languages. Hence duck typing: if it walks like a duck and talks like a duck... well, it might as well be a duck!

    Thanks for the soap box,
    Douglas
    Hello World

  7. #57
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    Which browsers have built-in functionality to run ruby script, or if it is not browser run, which servers are enabled by default?

    Excuse me, if the question is a kind of stupid, but Ruby is still a blackhole to me, and I'm trying to fill the abyss of ignorance

    Just like PHP, Lasso, ASP - Ruby is run server side.
    Not sure how friendly it is with Apache like PHP which runs as a mod.

    I suppose most Linux, OS X Windows platforms support Ruby no prob. I'm just starting to take a look at it - a friend showed it to me with Rails and it seems kind of interesting. Just don't know if I feel like porting my sites to Ruby just because its the next cool language.
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  8. #58
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian

    I was just pointing out that in JS, number literals (e.g. 1) aren't automatically converted to the Number object unless assigned to a variable, so 1.some_method() would never work.

    Well, this is not correct. Numbers are Objects, and you can always call methods for number literals:

    (1).add(2);

    As to operator overloading and generally ruby syntax compared to javascript: I think, javascript goes pascal way and supports only syntax which is neccessary. That's why it's compact and consistent. Ruby goes perl way and support any syntax which is possible. That's why it's bloated and confusing.

  9. #59
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woah, you're right, (1).add(2) does work. I guess that JS parsers don't understand 1.method and therefore need the parentheses, but the number literals still are Number objects.

    In any case, what I said is still technically true (1.add(2) will never work)

  10. #60
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Woah, you're right, (1).add(2) does work. I guess that JS parsers don't understand 1.method and therefore need the parentheses, but the number literals still are Number objects.
    Probably because they expect a number after a dot following a number, as in 3.14.

    That's one of the sad design errors in Javascript, to use same markers for different purposes. The same is with the overloaded '+' operator, used for two different purposes (numeric addition and string concatenation) depending on the context.

  11. #61
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, using dot for qualifying is pretty common, I wouldn't call it "design error". Javascript's "please-put-brackets-here" convention isn't significantly worse than ruby's "please-dont-use-empty-fraction" one. As to concatenation, many people (me too) agree, im my "dream language" I'd eliminate bit operators (noone really needs them) and use "&" for string concatenation, what results the nice orthogonal operator set.

    Generally, operator overloading (like " '-' * 5" mentioned above) is a Bad Thing.

    Code:
    #ruby
    
    def doublePrice(price)
       price * 2
    end
    
    puts doublePrice(2) # ok
    puts doublePrice("2") # WHAT??

  12. #62
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    >> def doublePrice(price)
    >>   price * 2 if price.is_a? Numeric
    >> end
    => nil
    >> puts doublePrice(2)
    4
    => nil
    >> puts doublePrice("2")
    nil
    => nil

  13. #63
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think, javascript goes pascal way and supports only syntax which is neccessary.
    That's not true. Array, Object, and RegExp literals are all unnecessary.

    The worst thing IMO about JS is the lack of a scoping operator. C++ has this:

    Code:
    int x = 1;
    {
      int x = 2;
      cout << x;  //2
    }
    cout << x;  //1
    JS has to fudge it up:

    Code:
    var x = 1;
    (function() {
      var x = 2;
      alert(x);  //2
    })();
    alert(x);  //1

  14. #64
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweatje

    Thanks, I'm aware . Even better would be probably

    Code:
    def doublePrice(price)
    	raise "Price must be a number" unless price.is_a? Numeric
    	price * 2
    end
    but I'd like to have the language do this boring job for me. If "*" were defined for numbers only, attempt to pass the string would end with "undefined method *" exception, and that would be a Good Thing.

    Quote Originally Posted by Maian
    That's not true.
    Of course, it depends on the definition of "necessity", which is absolutely personal.

  15. #65
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @stereofrog: I had this idea that there is no reason not to use empty string as the concatenation operator. Like (if it was so in PHP):
    PHP Code:
    echo "concatenate " return_from_a_function() " and a " $variable

  16. #66
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    I think, javascript goes pascal way and supports only syntax which is neccessary. That's why it's compact and consistent. Ruby goes perl way and support any syntax which is possible. That's why it's bloated and confusing.
    No syntax is neccessary, it is all there to help the programmer. If you truly like the minimum possible, try Ook or even BF

    Quote Originally Posted by stereofrog
    Of course, it depends on the definition of "necessity", which is absolutely personal.
    You say "necessity" but what you mean is "what I like to have" or even "what I'm used to"... there really is no way to argue with that

    Douglas
    Hello World

  17. #67
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    but I'd like to have the language do this boring job for me. If "*" were defined for numbers only, attempt to pass the string would end with "undefined method *" exception, and that would be a Good Thing.
    Code:
    class String
      undef :*
    end
    
    '4' * 2
    
    NoMethodError: undefined method `*' for "6":String
            from (irb):5
    Hello World

  18. #68
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Code:
    class String
      undef :*
    end
    Beat me to the punch

  19. #69
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Douglas, the minimal programming language, as everybody knows, is the Turing Machine. Strictly speaking, nothing more is neccessary to program a computer.

    class String
    undef :*
    end
    Thanks for example. And how do you express "unset * for everything other than number"? (oh yes, we ARE aware of ObjectSpace, please don't let the joke run too far )

    Berislav, using "nothing" as meaningful operator would definitely raise more problems than it seems to solve.

  20. #70
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Berislav, using "nothing" as meaningful operator would definitely raise more problems than it seems to solve.
    I never said I though it all over.

    Which problems would you expect?

  21. #71
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Douglas, the minimal programming language, as everybody knows, is the Turing Machine. Strictly speaking, nothing more is neccessary to program a computer.
    We were talking about syntax, not the 'size' of the language... and you can get languages which do less than a Turing Machine, they just aren't called "Turing complete" languges

    Thanks for example. And how do you express "unset * for everything other than number"?
    Probably using RJB,

    Code:
    str = Rjb::import('java.lang.String').new
    Bcause you don't seem to like dynamic languages anyway

    Berislav, using "nothing" as meaningful operator would definitely raise more problems than it seems to solve.
    He said white space, not 'nothing'... you might want to talk to the Python people about that

    Douglas
    Hello World

  22. #72
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    I never said I though it all over.

    Which problems would you expect?
    How are you going to interpret two subsequent spaces?

    should
    Code:
    puts "Two"  "spaces"
    print "Two spaces" or "Twospaces"?

  23. #73
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    How are you going to interpret two subsequent spaces?

    should
    Code:
    puts "Two"  "spaces"
    print "Two spaces" or "Twospaces"?
    The latter. As elsewhere in PHP (which I took for example), whitespace is ignored. Just like
    PHP Code:
    "Two" "spaces" 
    is the same as
    PHP Code:
    "Two"."spaces" 
    or even
    PHP Code:
    "Two"              .               "spaces" 
    It would be important to count your quotes, of course, or use a color-coding editor, but this would be perfectly valid syntax:
    PHP Code:
    "Two"""''""''"'''''''"""'spaces' 


    @DougBTX: I did say "empty string", but whitespace is more to the mark.

  24. #74
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like JS2 will not be including operator overloading but it was considered: http://www.mozilla.org/js/language/e...operators.html

  25. #75
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The guy who wrote Java also thought about them, but also tuned them down
    Hello World


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •