Buffer.isBuffer() in JS

1,542 views
Skip to first unread message

AJ ONeal

unread,
Dec 18, 2010, 4:44:35 PM12/18/10
to node.js mailing list
Any reason that there isn't a Buffer.isBuffer() method?

AJ ONeal

Liam

unread,
Dec 18, 2010, 4:58:00 PM12/18/10
to nodejs
from lib/buffer.js:

Buffer.isBuffer = function (b) {
return b instanceof Buffer;
};

Dean Landolt

unread,
Dec 18, 2010, 4:58:48 PM12/18/10
to nod...@googlegroups.com
On Sat, Dec 18, 2010 at 4:44 PM, AJ ONeal <cool...@gmail.com> wrote:
Any reason that there isn't a Buffer.isBuffer() method?


Here ya go:

Buffer.isBuffer = function(b) { return b instanceof Buffer; }

But yeah, instanceof checks have their downsides.

AJ ONeal

unread,
Dec 18, 2010, 6:48:55 PM12/18/10
to nod...@googlegroups.com
Thanks.
I might ought to take the liberty of forking the documentation since I seem to have a knack for finding these undocumented treasures.


BTW, JavaScript is remarkably consistent in it's methodologies, no?

typeof - broken
instanceof - broken
typeof + instanceof - might work, mostly broken
isTypeX - sometimes available, sometimes missing

AJ ONeal


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Liam

unread,
Dec 18, 2010, 7:30:41 PM12/18/10
to nodejs
You can submit docs requests as issues at https://github.com/miksago/node

How is instanceof broken?



On Dec 18, 3:48 pm, AJ ONeal <coola...@gmail.com> wrote:
> Thanks.
> I might ought to take the liberty of forking the documentation since I seem
> to have a knack for finding these undocumented treasures.
>
> BTW, JavaScript is remarkably consistent in it's methodologies, no?
>
> typeof - broken
> instanceof - broken
> typeof + instanceof - might work, mostly broken
> isTypeX - sometimes available, sometimes missing
>
> AJ ONeal
>
> On Sat, Dec 18, 2010 at 2:58 PM, Liam <networkimp...@gmail.com> wrote:
> > from lib/buffer.js:
>
> > Buffer.isBuffer = function (b) {
> >  return b instanceof Buffer;
> > };
>
> > On Dec 18, 1:44 pm, AJ ONeal <coola...@gmail.com> wrote:
> > > Any reason that there isn't a Buffer.isBuffer() method?
>
> > > AJ ONeal
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .

AJ ONeal

unread,
Dec 18, 2010, 7:45:59 PM12/18/10
to nod...@googlegroups.com
On Sat, Dec 18, 2010 at 5:30 PM, Liam <networ...@gmail.com> wrote:
You can submit docs requests as issues at https://github.com/miksago/node

How is instanceof broken?

That's the impression I got when looking at Crockford's typeOf. I don't remember why. I think arrays are instanceof Object in certain cases or something like that.

Mark S. Miller

unread,
Dec 18, 2010, 8:39:06 PM12/18/10
to nod...@googlegroups.com
    function f(){ return "hi"; }
    var nf = Object.create(f);
    nf instanceof Function; // prints true
    typeof nf; // prints 'object'.
    nf(); // throws TypeError: object is not a function

nf is indeed not a function. The reason instanceof returns true is merely because nf inherits from Function.prototype. That is all instanceof tests.

Neither does (typeof nf === 'function') mean that nf is a function, only that it is callable. For example, on some JavaScript implementations including v8, RegExps are still callable, and so

    typeof /x/; prints 'function'

The only reliable test is

    ({}).toString.call(f) === '[object Function]'; // prints true



 --
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.



--
    Cheers,
    --MarkM

Ryan Dahl

unread,
Dec 18, 2010, 9:45:14 PM12/18/10
to nod...@googlegroups.com
On Sat, Dec 18, 2010 at 3:48 PM, AJ ONeal <cool...@gmail.com> wrote:
> Thanks.
> I might ought to take the liberty of forking the documentation since I seem
> to have a knack for finding these undocumented treasures.


Yeah, sorry that wasn't documented. Now it is:
https://github.com/ry/node/commit/fca713eba45fb08d63dd677ca3163d002af15cca

Isaac Schlueter

unread,
Dec 18, 2010, 9:45:15 PM12/18/10
to nod...@googlegroups.com
You can use the Flanagan Device* and do this:

Buffer.isBuffer = function (b) {
return b &&
typeof b === "object" &&
Object.prototype.toString.call(b.parent) === '[object SlowBuffer]';
}

--i

* This is also sometimes referred to as the "Miller Device", but I saw
Mark set someone straight on that point.

Mark S. Miller

unread,
Dec 18, 2010, 9:51:29 PM12/18/10
to nod...@googlegroups.com
On Sat, Dec 18, 2010 at 6:45 PM, Isaac Schlueter <i...@izs.me> wrote:
You can use the Flanagan Device* and do this:

Buffer.isBuffer = function (b) {
 return b &&
       typeof b === "object" &&
       Object.prototype.toString.call(b.parent) === '[object SlowBuffer]';
}

--i

* This is also sometimes referred to as the "Miller Device", but I saw
Mark set someone straight on that point.

Indeed. I independently re-invented it years after David had already published it in JavaScript the Definitive Guide. "Flanagan Device" is indeed the right attribution.



--
    Cheers,
    --MarkM

Jorge

unread,
Dec 19, 2010, 5:22:01 AM12/19/10
to nod...@googlegroups.com
var prototypeA= {};
var prototypeB= {};
function ClassConstructor () {}
ClassConstructor.prototype= prototypeA;

var o= new Constructor;

o instanceof ClassConstructor
-> true
prototypeA.isPrototypeOf(o)
-> true
prototypeB.isPrototypeOf(o)
-> false

ClassConstructor.prototype= prototypeB;

o instanceof ClassConstructor
-> false
prototypeA.isPrototypeOf(o)
-> true
prototypeB.isPrototypeOf(o)
-> false

So to infer the "class" of a given "instance" object:

- the constructor/constructor.prototype/instance/instanceof relationship is brittle.

- the instance/prototype/isPrototypeOf is 100% solid, although it does not, either, for some "classes" of objects, completely define its behaviour.

(see Mark Miller's message)

Instances of Function and Array have added behaviour/special attributes built right into the instance object, in addition to the attributes inherited through their prototype objects/prototype chain, so for them, it's not enough to know what their prototypes are.
-- 
Jorge.

To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


-- 
Jorge.

Sent from my fridge

Jorge

unread,
Dec 19, 2010, 1:19:01 PM12/19/10
to nod...@googlegroups.com
On 19/12/2010, at 03:51, Mark S. Miller wrote:
> On Sat, Dec 18, 2010 at 6:45 PM, Isaac Schlueter <i...@izs.me> wrote:
> (...)

> * This is also sometimes referred to as the "Miller Device", but I saw
> Mark set someone straight on that point.
>
> Indeed. I independently re-invented it years after David had already published it in JavaScript the Definitive Guide. "Flanagan Device" is indeed the right attribution.

There's another common JavaScript-ish mis-attribution. Look at these 2 and tell me what known pattern you see in there that sounds familiar to you :

July 2003: hint : Object.prototype.clone
http://groups.google.com/group/comp.lang.javascript/msg/5d06e72e55d5bf11

July 2004: hint : function inheritObject ()
http://groups.google.com/group/comp.lang.javascript/msg/af7e608ae26dad03

Then go to http://archive.org, and see crockford.com in, say, March 2006 ( ~3 years later ) :
http://web.archive.org/web/20060301101236/www.crockford.com/javascript/

See any trace of beget() in there?. No. Not until mid-2006 -> "Prototypal Inheritance in JavaScript" -> "I have learned to fully embrace prototypalism, and have liberated myself from the confines of the classical model."

Personally, I have learnt almost everything I know about JS from Crockford ( if you read this, thanks! ), but perhaps it would be fair to spread the word to attribute the invention of Object.create() to Lasse Reichstein Nielsen. If you think so too, please help correct this mis-attribution as well. Thanks.
--
Jorge.

Reply all
Reply to author
Forward
0 new messages