[Prototype-core] Re: bracket notation

2007-06-22 Thread Andrew Dupont


On Jun 21, 2:35 pm, sed [EMAIL PROTECTED] wrote:
 I'm not sure why this worked before, it must be related to the html/
 script being generated into the div from the ajax.updater rather than
 just being on the page.

Nope. It's sillier than that. Let's say you've got an element with an
ID of foo on the page:

var f = new String(foo);
$(f); //- null
document.getElementById('foo') //- [object HTMLElement]

This can be traced to JavaScript's bizarre String constructor --
`typeof f` returns object, not string. Looking at `f` in Firebug
will reveal ['f', 'o', 'o']. This is a crack in the object/primitive
duality of types like String and Number.

The $ function uses the typeof operator to figure out if the passed
argument is a string. Since it doesn't appear to be one, it doesn't
get run through document.getElementById.

You've found the solution already -- the new String call is
superfluous.

Cheers,
Andrew


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Miha Zimin

Hi guys. I'm new in prototypejs and script.aculo.

But, lets consider Andrew Dupont's example again:

var f = new String(foo);
alert($(f)) //- foo
alert(document.getElementById('foo')) //- [object HTMLElement]

It has cause:

When String( ) is used as a constructor with the new operator, it
returns a String object, which holds the string s or the string
representation of s. When the String( ) constructor is used without
the new operator, it simply converts s to a primitive string and
returns the converted value.

it's mean:

alert(typeof new String()) //- object
alert(typeof String()) //- string

Possible, it will be better to redefine $ like this:

function $(element) {
  if (arguments.length  1) {
for (var i = 0, elements = [], length = arguments.length; i 
length; i++)
  elements.push($(arguments[i]));
return elements;
  }
  if (typeof element == 'string' || element instanceof String) //-
NEW CONDITION
element = document.getElementById(element);
  return Element.extend(element);
}

Or it isn't good idea. What do you think?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Mislav Marohnić
On 6/22/07, Miha Zimin [EMAIL PROTECTED] wrote:


 Or it isn't good idea. What do you think?


Leave it as it is. The String function/constructor is maybe useful for other
things, but seldom for handling element IDs before passing them to the $
function. The current check is enough - it was enough ever since Prototype
started.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Tobie Langel

 The String function/constructor is maybe useful for other things [...]

Like what exactly !?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Mislav Marohnić
On 6/22/07, Tobie Langel [EMAIL PROTECTED] wrote:


  The String function/constructor is maybe useful for other things [...]

 Like what exactly !?


Like when casting something to string when toString is not sufficient. I
don't know when that may be.

I was just stating this: although this may be a feature of the JS language
that has real-world usage, I sincerely doubt that its usage applies to
short, simple strings that are element IDs.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread jdalton



I don't see the harm in adding the second condition element
instanceof String
you could use :

if(element  element.constructor == String)

This works for both foo and new String(foo);

I find that it is faster than using typeOf.
I have made very JavaScript heavy applications and noticed that using
typeOf value == 'Object' and others is slow
compared to myObject.constructor, especially when called a few
thousand times.

Also I don't buy the it hasn't been fixed since the beginning, so why
fix it now? argument.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Mislav Marohnić
On 6/22/07, jdalton [EMAIL PROTECTED] wrote:


 Also I don't buy the it hasn't been fixed since the beginning, so why
 fix it now? argument.


Well, if in almost 2 years we have a first report about it only now, then it
sounds like a big edge case to me.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Mislav Marohnić
On 6/22/07, Tobie Langel [EMAIL PROTECTED] wrote:


 I'm actually getting faster results in Firefox for typeof in that
 particular case.


Me too. Try it for yourself:
http://gfx.neohub.com/benchmark/creator.html

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread Tobie Langel

I'm actually getting faster results in Firefox for typeof in that
particular case.

On Jun 22, 8:55 am, jdalton [EMAIL PROTECTED] wrote:
 I don't see the harm in adding the second condition element
 instanceof String
 you could use :

 if(element  element.constructor == String)

 This works for both foo and new String(foo);

 I find that it is faster than using typeOf.
 I have made very JavaScript heavy applications and noticed that using
 typeOf value == 'Object' and others is slow
 compared to myObject.constructor, especially when called a few
 thousand times.

 Also I don't buy the it hasn't been fixed since the beginning, so why
 fix it now? argument.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread jdalton

If you run it more than once you get different results. Reguardless of
this benchmark (I got it to show .constructor was faster consistently
more times, IE)
Its a method of detecting a string that will fix the issue.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-22 Thread jdalton

And using : http://gfx.neohub.com/benchmark/creator.html

Firefox: slower by alot using constructor
IE : faster by alot using constructor

return ('hi'.constructor == String);
vs
return (typeof 'hi' == 'string');

IE: faster by alot using constructor
Firefox : mixed (sometimes slower sometimes faster)

return ({}.constructor == Object);
vs
return (typeof {} == 'object');

I have tried both in my applications and dont notice a speed decrease
in firefox (using firebug) that is notable.



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-21 Thread Mislav Marohnić
On 6/21/07, sed [EMAIL PROTECTED] wrote:


 I've posted this question to ruby on rails, but figured people here
 would be more likely to know the answer.

 Using bracket notation in javascript seems to stop workiing when
 prototype is included.


I'm not using the notation, but if Prototype breaks it it's a serious issue.

Can you pastie us a minimal document that demonstrates this?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-21 Thread Ryan Johnson

This is very very unlikely. I use bracket notation with and without
prototype on many scripts that work cross browser. Check which other
libraries you are including, and make sure it is not a problem or
missing attribute of the object you are working with.

On Jun 21, 8:41 am, sed [EMAIL PROTECTED] wrote:
 I've posted this question to ruby on rails, but figured people here
 would be more likely to know the answer.

 Using bracket notation in javascript seems to stop workiing when
 prototype is included.

 e.g. document.main[name].value. Is this a know issue? I know using $
 ('name').value is the best, but it would seem to be a bug for
 prototype to break any existing javascript/dom object access methods.
 Thanks.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: bracket notation

2007-06-21 Thread sed

Interesting problem here and I don't think it's a prototype bug, but I
corrected the issue.
Here is what was happening if anyone's interested Here is a short
explanation:

Ajax Call generates some html into a div with this function inside:

GetNextField = function() {return('thenextfield');};

In an included .js file there is another function that goes like so:
function NextField()
{
  FieldName = new String(GetNextField());
  // then doing so breaks
  $(FieldName).focus() or document.main[FieldName].focus();
}

It's the 'new String' that's breaking it, but prior to adding
prototype and dumping all this into a layer with ajax it worked just
fine

The fix:
FieldName = GetNextField();
Bracket and prototype notation both work.

I'm not sure why this worked before, it must be related to the html/
script being generated into the div from the ajax.updater rather than
just being on the page.

Definitely not a prototype problem.

On Jun 21, 12:53 pm, Ryan Johnson [EMAIL PROTECTED] wrote:
 This is very very unlikely. I usebracketnotation with and without
 prototype on many scripts that work cross browser. Check which other
 libraries you are including, and make sure it is not a problem or
 missing attribute of the object you are working with.

 On Jun 21, 8:41 am, sed [EMAIL PROTECTED] wrote:



  I've posted this question to ruby on rails, but figured people here
  would be more likely to know the answer.

  Usingbracketnotation in javascript seems to stop workiing when
  prototype is included.

  e.g. document.main[name].value. Is this a know issue? I know using $
  ('name').value is the best, but it would seem to be a bug for
  prototype to break any existing javascript/dom object access methods.
  Thanks.- Hide quoted text -

 - Show quoted text -


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---