John G Harris said:The section 'shuffle the Array' looks rather dodgy.
Indeed. The "algorithm" is:
list = list.sort(function() Math.random() - 0.5)
What will happen depends on the underlying sorting algorithm, the only
thing that's safe to assume is that the shuffling will not have
an (approximatly) even distribution of the possible permutations.
If, e.g., it uses insertion sort (often used for small arrays), the
last element has a 50% chance of staying where it is, and 25% chance
of moving only one position.
It's not even remotely usable for actual shuffling.
(Not to mention that I've seen at least one sort implementation that
that didn't necessarily terminate for a non-stable comparison!)
Jorge said:
Fri said:Indeed. The "algorithm" is:
list = list.sort(function() Math.random() - 0.5)
http://code.google.com/p/jslibs/wiki/JavascriptTips ....
Floating to integer
(123.345456).toFixed(); // is: 123
typeof (1.5).toFixed(); // is: string
You could do this as a faster alternative:
~~123.345456
In Ecma262 Number is double-precision 64-bit format IEEE 754. You
missed one general point. Bitwise operators cast Number to ToInt32();
So you can overflow 32 bits, and get unexpected results.
Math.floor(123.345456); //123
123.345456 - (123.345456 % 1); //123
Regards.
AIUI, it means the object has no own properties.Asen said:| function isNotEmpty(obj) {
| for ( var tmp in obj )
| return true
| }
What are you mean with that function "isNotEmpty"? For-in look up in
prototype chain and enumerate properties who's doesn't have attribute
{DontEnum}. In ECMA262 i don't know `object' who is empty, because
inherit from Object.prototype.
The Singleton pattern:Jorge said:
Correction:Garrett said:The Singleton pattern: [snip]Jorge said:
// Usage:
var AAA = createSingleton(
function(name) {
return AAA;
function AAA(name) {
this.name = name;
this.method = meth;
}
function meth(){
return 17;
}
}
);
AIUI, it means the object has no own properties.
The example has the footnote:
| Â The aim of this function is to detect properties directly on obj.
It fails if the program modifies Object.prototype. That can and should
avoided.
It would be safer to replace this with:
if (Object.prototype.hasOwnProperty.call(o, i)) { /* ... */ }
(probably also aliasing `Object.prototype.hasOwnProperty` for faster access)
var o = { 'hasOwnProperty': 'foo' };
o.hasOwnProperty('bar'); // TypeError
You can try testing for it, but why bother.
`Object.prototype.hasOwnProperty.call` doesn't have these problems.
... that has {DontEnum} set in IE. See table in
<http://groups.google.com/group/comp.lang.javascript/msg/dda4dee3390fa71a>
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.