The Artima Developer Community
Sponsored Link

Python Buzz Forum
Prototype.js and Object.prototype

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ian Bicking

Posts: 900
Nickname: ianb
Registered: Apr, 2003

Ian Bicking is a freelance programmer
Prototype.js and Object.prototype Posted: Jul 15, 2005 12:39 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ian Bicking.
Original Post: Prototype.js and Object.prototype
Feed Title: Ian Bicking
Feed URL: http://www.ianbicking.org/feeds/atom.xml
Feed Description: Thoughts on Python and Programming.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ian Bicking
Latest Posts From Ian Bicking

Advertisement

I hadn't seen a problem with Prototype's Object.prototype.extend method until recently, when I added Xinha support to the Paste filebrowser example, and it broke Xinha. I would have actually been mystified about the breakage, if I hadn't read Bob's rant on it (and another critique of Object.prototype).

Here's the code in question:

Object.prototype.extend = function(object) {
  for (property in object) {
    this[property] = object[property];
  }
  return this;
}

Here's what I first replaced it with (and this didn't work!):

function extend(ob1, ob2) {
  for (property in ob2) {
    ob1[property] = ob2[property];
  }
  return ob1;
}

The problem was that methods became unbound, and Prototype uses .extend({...}) to do something subclass-like. I'm personally highly confused about how functions become bound to objects. In this case, this would be set to window instead of ob1 (window is the browser's global object and default this). But this version worked:

function extend(ob1, ob2) {
  return (function(object) {
    for (property in object) {
      this[property] = object[property];
    }
    return this;
  }).apply(ob1, [ob2]);
}

And I modified prototype to make use of the function. I don't know if I'll continue to use Prototype or not -- there's an explosion of Javascript libraries, and it's very hard to choose. Some people's priorities are different than mine (for instance, comparison hasn't come up for me as an issue, and Dojo's <http://dojotoolkit.org/> obsession with transports doesn't make sense to me either, and anything with server-side integration is a complete non-starter for me). I'll keep experimenting.

Explanations or pointers to exactly how and when this gets bound would be of interest to me, if anyone has seen them. It seems to be highly contextual -- there's nothing like Python's BoundMethod.

Read: Prototype.js and Object.prototype

Topic: The Tens of Commandments Previous Topic   Next Topic Topic: Ajax Experimentations: Filebrowser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use