From: Garrett Smith on
Asen Bozhilov wrote:
> On 29 Окт, 23:03, Jorge <jo...(a)jorgechamorro.com> wrote:
>> http://code.google.com/p/jslibs/wiki/JavascriptTips
>>
>> --
>> Jorge.
>
> | 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.
>
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.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: kangax on
Garrett Smith wrote:
> Asen Bozhilov wrote:
>> On 29 Окт, 23:03, Jorge <jo...(a)jorgechamorro.com> wrote:
>>> http://code.google.com/p/jslibs/wiki/JavascriptTips
>>>
>>> --
>>> Jorge.
>>
>> | 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.
>>
> 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.

And fails to work around JScript bug.

One could also feature-test and use non-standard __count__ for this
purpose, but that might not be very reliable (e.g. an object can have
__count__ property with numeric value that has nothing to do with "real"
__count__ — number of own properties).

Mozilla, btw, seems to make __count__ {ReadOnly, DontDelete, DontEnum}.
Once you create an object, __count__ can't be changed or deleted.
However, it's possible to give object "usual" __count__ property via
object initializer:

(function(){
var o = { "__count__": 10 };
var o2 = { };
o2['__count__'] = 20;
return [o.__count__, o2.__count__]; // returns [10, 0]
})();

--
kangax
From: Garrett Smith on
Jorge wrote:
> http://code.google.com/p/jslibs/wiki/JavascriptTips
>
The Singleton pattern:
| function MySingletonClass() {
|
| if ( arguments.callee._singletonInstance )
| return arguments.callee._singletonInstance;
| arguments.callee._singletonInstance = this;
|
| this.Foo = function() {
| // ...
| }
| }

There are a few downsides to that approach.

The singleton logic exists inside the constructor. A public
_singletonInstance property is exposed. Using arguments.callee prevents
an optimization in some implementations. Everything being in the
constructor can lead to a long constructor (most find long
methods or constructors harder to read and debug.

An alternative Singleton:

function createSingleton(buildCtor){
var instance;
return {
getInstance : getInstance
};
function getInstance(arg){
if(!instance) {
var ctor = buildCtor();
ctor.prototype = null;
instance = new ctor(arg);
}
return instance;
}
}

// Usage:
var AAA = createSingleton(
function(name) {
return AAA;
function AAA(name) {
this.name = name;
this.method = meth;
}
function meth(){
return 17;
}
}
);

That is similar to, though much simpler than APE.createFactory.

Longer scope chain lookup can be mitigated by aliasing local variables
in the scope of the function that return the constructor. Care must be
taken so as to not store reference to things that are not needed in
scope.

Those aliased variables benefit from fast lookup and munging (where
relevant). Those variables are not initialized unless AAA.getInstance is
called, e.g.:-

var AAA = createSingleton( ...

- is 1 function call (to createSingleton), the creation of one function
(the constructor) the creation of an object with a getInstance method,
plus two [[Scope]] objects (for each function).

1 object, 2 functions, 1 function call.

The |constructor| property is replaced with Object (the [[Prototype]] is
Object.prototype), making the Singleton truly private. With one less
object in the prototype chain, for-in enumeration will have less work.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Garrett Smith on
Garrett Smith wrote:
> Jorge wrote:
>> http://code.google.com/p/jslibs/wiki/JavascriptTips
>>
> The Singleton pattern:
[snip]

> // Usage:
> var AAA = createSingleton(
> function(name) {
> return AAA;
> function AAA(name) {
> this.name = name;
> this.method = meth;
> }
> function meth(){
> return 17;
> }
> }
> );
Correction:
the |name| parameter of the outer anonymous function (the function that
gets passed as buildCtor) is unnecessary.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Asen Bozhilov on
On 1 Ноем, 07:31, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:

> 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.
> --
> Garrett
> comp.lang.javascript FAQ:http://jibbering.com/faq/

Of course. You have in APE one method `mixin' who copy own properties.
Here we can use similar approach.

Object.hasOwnProperties = function(o)
{
for (var i in o)
{
if (o.hasOwnProperty(i)) return true;
}
return false;
};

Except {DontEnum} bug of JScript for properties `valueOf' and
`toString'.

Regards.