Any reason why this might not work?

26 views
Skip to first unread message

Walter Lee Davis

unread,
Aug 7, 2012, 2:29:56 PM8/7/12
to prototype-s...@googlegroups.com
I have a function that might need to run under 1.6 or 1.7, but I don't want to branch my code horribly around store and retrieve. This seems to work:

if(typeof Element.store == 'undefined'){
Element.addMethods({
store: function(elm, k, v){
elm[k] = v;
},
retrieve: function(elm, k){
return (elm[k]);
}
});
}

Can anyone spot any reason why it might fail horribly in some browser?

Walter

Jason Westbrook

unread,
Aug 7, 2012, 5:31:04 PM8/7/12
to prototype-s...@googlegroups.com

Internet explorer doesn't like object definitions without quotes around the names


if(typeof Element.store == 'undefined'){
  Element.addMethods({
    "store": function(elm, k, v){
      elm[k] = v;
    },
    "retrieve": function(elm, k){
      return (elm[k]);
    }
  });
}

Jason Westbrook | T: 313-799-3770 | jwest...@gmail.com




Walter

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.


Victor

unread,
Aug 9, 2012, 5:28:31 AM8/9/12
to prototype-s...@googlegroups.com
It may fail because this code may overwrite element's properties (and in some browsers like IE and Opera some attributes, mapped as element properties). E.g. $("someInput").store("name", "foo").store("type", "bar"); Better will be some separate container to store values inside, but this leads to back-porting element storage back to Prototype 1.6:

// storage container nested in each element
if(typeof Element.store == 'undefined'){
  Element.addMethods({
    store: function(e, k, v){
      e._prototypeStorage = e._prototypeStorage || {};
      e._prototypeStorage[k] = v;
    },
    retrieve: function(e, k){
      e._prototypeStorage = e._prototypeStorage || {};
      return (e._prototypeStorage[k]);
    }
  });
}

// storage container in closure
(function(){
if(typeof Element.store == 'undefined'){
  var storage = {};
  function uid(e) {
    // TODO return some uid for element like in Prototype 1.7
  }
  Element.addMethods({
    store: function(e, k, v){
      var id = uid(e);
      storage[id] = storage[id] || {};
      storage[id][k] = v;
    },
    retrieve: function(e, k){
      var id = uid(e);
      storage[id] = storage[id] || {};
      return (storage[id][k]);
    }
  });
}
})();

Victor

unread,
Aug 9, 2012, 5:34:01 AM8/9/12
to prototype-s...@googlegroups.com
Internet explorer doesn't like object definitions without quotes around the names

Hmm... example from Prototype 1.7 sources:

  Object.extend(methods, {
    getStorage: getStorage,
    store:      store,
    retrieve:   retrieve
  });

Quotes are required for properties matching reserved words (class etc.) or containing characters forbidden in identifiers (spaces, colons etc.), and browser name/version doesn't matter.

Walter Lee Davis

unread,
Aug 9, 2012, 10:23:37 AM8/9/12
to prototype-s...@googlegroups.com

On Aug 9, 2012, at 5:28 AM, Victor wrote:

> It may fail because this code may overwrite element's properties (and in some browsers like IE and Opera some attributes, mapped as element properties).

That's an excellent point.

> E.g. $("someInput").store("name", "foo").store("type", "bar"); Better will be some separate container to store values inside, but this leads to back-porting element storage back to Prototype 1.6:
>
> // storage container nested in each element
> if(typeof Element.store == 'undefined'){
> Element.addMethods({
> store: function(e, k, v){
> e._prototypeStorage = e._prototypeStorage || {};
> e._prototypeStorage[k] = v;
> },
> retrieve: function(e, k){
> e._prototypeStorage = e._prototypeStorage || {};
> return (e._prototypeStorage[k]);
> }
> });
> }

Would this construction:

e._prototypeStorage = e._prototypeStorage || {}

fall afoul of the issue that Jason pointed out? Would IE bork the assignment because the key wasn't in hash form and quoted?

Walter

>
> // storage container in closure
> (function(){
> if(typeof Element.store == 'undefined'){
> var storage = {};
> function uid(e) {
> // TODO return some uid for element like in Prototype 1.7
> }
> Element.addMethods({
> store: function(e, k, v){
> var id = uid(e);
> storage[id] = storage[id] || {};
> storage[id][k] = v;
> },
> retrieve: function(e, k){
> var id = uid(e);
> storage[id] = storage[id] || {};
> return (storage[id][k]);
> }
> });
> }
> })();
>
> --
> You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/TH_WyncdHx0J.

Jason Westbrook

unread,
Aug 9, 2012, 10:58:07 AM8/9/12
to prototype-s...@googlegroups.com

I agree that some code is not always written like that - but I've had many instances where a javascript block would work in modern standards compliant browsers yet fail silently in IE until I put quotes in, single or double.

Jason Westbrook | T: 313-799-3770 | jwest...@gmail.com



--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.

Victor

unread,
Aug 10, 2012, 3:18:09 AM8/10/12
to prototype-s...@googlegroups.com
Would this construction:
e._prototypeStorage = e._prototypeStorage || {}
fall afoul of the issue that Jason pointed out? Would IE bork the assignment because the key wasn't in hash form and quoted?

It will work fine. I've not found any specific property name which will require quotes ONLY in IE - just use characters [A-Za-z_$] and such property name will not require quotes (except of course reserved javascript words).

Victor

unread,
Aug 10, 2012, 3:19:16 AM8/10/12
to prototype-s...@googlegroups.com
I've had many instances where a javascript block would work in modern standards compliant browsers yet fail silently in IE until I put quotes in, single or double.

Please, show any example of such code.
Reply all
Reply to author
Forward
0 new messages