Starting to Build a JavaScript Library

P

Peter Michaux

Try the |in| operator.
var xhr = new ActiveXObject('Microsoft.XMLHTTP');
'open' in xhr;

That's an interesting suggestion. The typeof test gives a little more
information, though.
Or, if you don't like |in|, you could use:

Object.prototype.hasOwnProperty.call(xhr, 'open')

I don't know that sending a host object to a JavaScript function that
expects a JavaScript object is a good idea. That isn't a totally
coherent sentence but it makes me nervous anyway. The typeof test
gives more information here also.

Thanks,
Peter
 
D

David Mark

[snip]
Now I need a test to make sure the constructor property is set
correctly.
I don't ever use the constructor property and I doubt many folks do.
It isn't worth the trouble to keep it "correct".
Then again, I never overwrite the initial prototype property of a
function anymore so I'm not screwing up the constructor property in
the first place.
Are you persisting with FORK?  Or have you decided that the "code
worth recommending project" (cwrp) will form the basis of a (new?)
modular FORK?
Perhaps dhtml can use the cwrp as the basis for a modular library else
it seem the effort is already forked (gosh, my puns are nearly as bad
as Linus T's).
I don't quite know what will happen yet. David seems to be working on
Things are as they seem in this case.  Did you check out the Alpha I
posted a month back?

I have been reading the code which has been a big help. The builder is
a great feature.

Glad to hear it. I have heard positive things from others as well,
despite the fact that there were numerous problems with the Alpha
(most related to the management of dependencies by the builder.) What
I am arbitrarily calling the Beta is much improved and is much larger
in scope (I know that is an ill-advised design strategy, but this
project is a hobby for me.)
How do you feel about promoting your library? With Fork, I found out

I don't have anything special planned. I certainly hope that those
who find it useful "digg" it (or whatever) for others to discover. My
primary purpose for writing it is eliminate the "there's nothing
better out there" excuse for aspiring application developers.
almost instantly that I didn't want to promote it aggressively. Folks
that run the YUI, jQuery, Dojo, Prototype libraries go to a lot of
conferences and have birthday parties, etc. Some of the libraries have

I don't go to any conferences. I do occasionally have birthday
parties, but most (if not all) who attend have never heard of
JavaScript.
people designated as evangelists, I believe. I don't have a problem

I won't be handing out such designations. People can say what they
will.
with any of that and it is why the libraries are popular.

That and the fact that most of them are equally execrable. I think it
is a "pick your poison" mentality that drives people to use these
things.
Theoretically popular means more people reading the code and reporting
bugs. Those projects do have bug fix releases and so that part is
working.

Working at a snail's pace for most. And at worst, waffling rather
than working. I don't consider swapping out browser sniffs to be bug
fixes.
Indeed there are. It is working fine for me because I only use it on a
site behind login with tech-savvy users. No complaints.

It looked suitable for use on the public Internet to me. Certainly
more so than things like Prototype and jQuery. As you don't sniff
browsers, the code will probably not require major changes as time
goes on.
I like the API in general but would drop the namespacing to just one
level. I don't know why I can't mentally just go to global functions
like FORK_addistener. What is wrong with that anyway?

For one, some browsers (e.g. Opera) are slower to resolve global
identifiers. For two, imagine if you had hundreds of functions.
Granted, the FORK_ prefix would likely prevent collisions, but such
precautions aren't required when using a single global object as a
namespace.

I'll check it out.
Thanks for all the help. I'm interested to see if people will read it
and comment.

How are you promoting it?
 
P

Peter Michaux

[snip]
How do you feel about promoting your library? With Fork, I found out

I don't have anything special planned. I certainly hope that those
who find it useful "digg" it (or whatever) for others to discover. My
primary purpose for writing it is eliminate the "there's nothing
better out there" excuse for aspiring application developers.

I don't think this will make it popular. I believe people want to use
a library that many other people are using so they don't commit to
using a project and then the project dies. Hype works for building
library popularity by building the necessary sense of momentum. I know
you will be using your code so it won't just up and die but other
people don't know that.

For one, some browsers (e.g. Opera) are slower to resolve global
identifiers.

But resolving FORK_addListener is faster than resolving
FORK.addListener because FORK is global and then a property of FORK
must be found. If a local copy of the function is kept in some scope
then there is no difference.

For two, imagine if you had hundreds of functions.
Granted, the FORK_ prefix would likely prevent collisions,

The MM_ functions are still out there going strong and unclobbered.

but such
precautions aren't required when using a single global object as a
namespace.

Actually I think that FORK_addListener is less likely to be clobbered
than FORK. It's all a crap shoot with this single global namespace
business. I'm just glad I don't inject my code into mashups with seven
libraries running simultaneously.
I'll check it out.

I just posted it. You'll notice I changed the feature detection for
getElementsByTagName. I think that was the only feature test that made
me nervous because I was assuming the document.all fallback for '*'
tagName. Sure enough when I tested in Safari 1.0 the whole widget
crapped out. Safari 1.0 has getElementsByTagName, doesn't support '*',
and doesn't have the document.all fallback. It just goes to show that
it really is necessary to test everything.

I have to read the article through a couple times while it is live.
Somehow little things pop out at me when I finally see it live.

By the way, the comments in the feature detection article really show
how little people know about the topic. I removed the comment thread
about c.l.js from that article because I really don't want a flame war
on my blog. That is the beauty of moderation. It would be awful to
have moderation in c.l.js (even though people could be friendlier) but
on my blog I just don't want to have that going on.

How are you promoting it?

When I write an article I think might interest others I email the
ajaxians and tell them about it. I think I've done this four times.
They have a news email link down in the right column of http://ajaxians.com.
When they post something to their page that usually means 1500 to 2000
visitors to the article in a single day. Some people have mentioned my
articles on reddit and that usually sends 1000 to 1500 people per day
while it is on the front page. The articles that get linked these ways
also stay the most popular articles for ever after.

Peter
 
D

David Mark

[snip]
How do you feel about promoting your library? With Fork, I found out
I don't have anything special planned.  I certainly hope that those
who find it useful "digg" it (or whatever) for others to discover.  My
primary purpose for writing it is eliminate the "there's nothing
better out there" excuse for aspiring application developers.

I don't think this will make it popular. I believe people want to use

I don't intend to expend any effort to make it popular. It will be or
it won't.
a library that many other people are using so they don't commit to

You start with a couple of users, then they tell two friends, who tell
two friends, etc. If the inherent virality of the Web fails to
promote what I consider a superior mousetrap, then we will likely be
overrun by mice in the future. I'm not too worried about it.
using a project and then the project dies. Hype works for building

If the project is well-documented and relatively future-proof (I need
to work on the former), then it will never die.
library popularity by building the necessary sense of momentum. I know

I don't plan to utilize dedicated evangelists, paid spokesmen or
shills. The project will have to build momentum on its own volition.
you will be using your code so it won't just up and die but other
people don't know that.

I will endeavor to ensure that people at least know that.
But resolving FORK_addListener is faster than resolving
FORK.addListener because FORK is global and then a property of FORK
must be found. If a local copy of the function is kept in some scope
then there is no difference.

That's true. But then you have to create an internal "shortcut"
reference for every function (not just those exposed as part of the
API.)
The MM_ functions are still out there going strong and unclobbered.

Next time I see one of those miserable things, I am going to clobber
it. But seriously, those are always the first things I crop out of
sites when performing a salvage operation. They are all worthless,
outdated nonsense.
Actually I think that FORK_addListener is less likely to be clobbered
than FORK. It's all a crap shoot with this single global namespace

But if you have hundreds of targets instead of one, then the
probability of collisions is increased. I do see your point that
"FORKINGLEAVETHISALONE" would be a less likely candidate for an
overwrite.
business. I'm just glad I don't inject my code into mashups with seven
libraries running simultaneously.

I think that goes without saying. But theoretically you should be
able to co-exist with most libraries.
I'll check it out.

I just posted it. You'll notice I changed the feature detection for
getElementsByTagName. I think that was the only feature test that made
me nervous because I was assuming the document.all fallback for '*'

Certainly the similar code in CWR doesn't make such an assumption. I
know the original version I wrote detected document.all.
tagName. Sure enough when I tested in Safari 1.0 the whole widget
crapped out. Safari 1.0 has getElementsByTagName, doesn't support '*',
and doesn't have the document.all fallback. It just goes to show that

In that case, you should get an invalid result back from getEBTN when
passing "*" for the tag. It is up to the application to deal with
that anomaly (i.e. bail out or abort from the start.) Personally, I
never query for "*" as I know that it is prone to errors and the
workaround is not universally available.
it really is necessary to test everything.

Why are you passing "*" to getEBTN anyway?
I have to read the article through a couple times while it is live.
Somehow little things pop out at me when I finally see it live.

I'll comment on it when I get a chance. Probably tomorrow.
By the way, the comments in the feature detection article really show
how little people know about the topic. I removed the comment thread
about c.l.js from that article because I really don't want a flame war
on my blog. That is the beauty of moderation. It would be awful to
have moderation in c.l.js (even though people could be friendlier) but
on my blog I just don't want to have that going on.

Do you mean the person who commented twice in a row with virtually the
same sentiment about rude behavior? How rude was that?
When I write an article I think might interest others I email the
ajaxians and tell them about it. I think I've done this four times.

The Ajaxians have landed?! Are they friendly?
They have a news email link down in the right column ofhttp://ajaxians.com..
When they post something to their page that usually means 1500 to 2000
visitors to the article in a single day. Some people have mentioned my
articles on reddit and that usually sends 1000 to 1500 people per day
while it is on the front page. The articles that get linked these ways
also stay the most popular articles for ever after.

Do you add links to social bookmarking sites as well? They seem to be
a popular way of sharing resources. Personally, I have never even
signed onto one of them, but I may so I can "digg" my own pages. As
far as Website promotion goes, that's about the most effort I am
willing to expend.
 
P

Peter Michaux

On Feb 19, 1:14 am, Peter Michaux <[email protected]> wrote:
Do you add links to social bookmarking sites as well?

No. I just email Ajaxian and that is it. I'm not an evangelist other
than what I write. A big part of writing the articles is to get the
ideas straight in my head. Once I do that I want to move on to
learning something new. You know, this whole tabbed pane article has
just been a way to procrastinate writing a new function for Emacs. I
really don't want to write this one. It is a real pain. But I suppose
it is back at the top of the heap now :-( Seriously I've wanted to
write the tabbed pane article for a long time. It was going to be
about something totally different though: the different architectures
to write a widget like using closures or OOP.

Peter
 
D

dhtml

You may like to look at some of this code



I don't think that level of complexity in production is a good idea.
Work harder in development and have better performance in production.


Why? Most programming environments require a build step.

I don't think the monolith style library is a good idea.

------------

You need a very clear idea of which browsers you are targeting or is
your code for the general web? Then you need a plan for feature
detection. David Mark has been going through his system with the group
for a few months now. I've summarized it as an article just yesterday

<URL:http://peter.michaux.ca/article/7146>
// version 3
function isHostMethod(object, property) {
return !!(typeof object[property]) ||
// For compliant objects which have a typeof
// result which is an empty string.
//
// Would error for ActiveX but
// will have returned already.
object[prototype];
}

isHostMethod(document, 'foo')

Is clearly broken.
It gets to

typeof object[property], that's a string.
!converts to false,
!! converts to true

the method returns true. always.

If it were to get to the second part,
object[prototype]

would cause an referenc error.

Garrett
 
P

Peter Michaux

You may like to look at some of this code
I don't think that level of complexity in production is a good idea.
Work harder in development and have better performance in production.
Why? Most programming environments require a build step.
I don't think the monolith style library is a good idea.

You need a very clear idea of which browsers you are targeting or is
your code for the general web? Then you need a plan for feature
detection. David Mark has been going through his system with the group
for a few months now. I've summarized it as an article just yesterday

// version 3
function isHostMethod(object, property) {
return !!(typeof object[property]) ||
// For compliant objects which have a typeof
// result which is an empty string.
//
// Would error for ActiveX but
// will have returned already.
object[prototype];

Also note my typo ^^^^^^^^^
}

isHostMethod(document, 'foo')

Is clearly broken.
It gets to

typeof object[property], that's a string.
!converts to false,
!! converts to true

the method returns true. always.

Thanks for pointing this out. This is not the function I meant to
post. What I meant was the following, not to say that I haven't made a
mistake here.

function isHostMethod(object, property) {
return typeof object[property] == 'unknown' ||
// Would error for ActiveX but
// will have returned already.
object[property];
}




If it were to get to the second part,
object[prototype]

would cause an referenc error.

I don't think that is true. It is ok to access an undefined property
of an object. What is not ok is referencing a undeclared variable.

Thanks again,
Peter
 
H

Henry

On Feb 16, 2:19 pm, Peter Michaux wrote:
// version 3
function isHostMethod(object, property) {
return !!(typeof object[property]) ||
// For compliant objects which have a typeof
// result which is an empty string.
//
// Would error for ActiveX but
// will have returned already.
object[prototype];

}

isHostMethod(document, 'foo')

Is clearly broken.
It gets to

typeof object[property], that's a string.
!converts to false,
!! converts to true

No, the empty string has falseness (and is the only string primitive
value for which that is the case). The NOT operator converts the empty
string to boolean true, and double NOT will result in boolean false
when the subject is an empty string.
the method returns true. always.
<snip>

Not always.
 
P

Peter Michaux

On Feb 16, 2:19 pm, Peter Michaux wrote:
// version 3
function isHostMethod(object, property) {
return !!(typeof object[property]) ||
// For compliant objects which have a typeof
// result which is an empty string.
//
// Would error for ActiveX but
// will have returned already.
object[prototype];

isHostMethod(document, 'foo')
Is clearly broken.
It gets to
typeof object[property], that's a string.
!converts to false,
!! converts to true

No, the empty string has falseness (and is the only string primitive
value for which that is the case). The NOT operator converts the empty
string to boolean true, and double NOT will result in boolean false
when the subject is an empty string.

This is what I was thinking at the time and of course forgot that
moments ago. Regardless the test above is not very useful. If the
typeof results is "undefined" then the function returns true and I
still wouldn't know if I can call the property.

Peter
 
T

Thomas 'PointedEars' Lahn

Peter said:
String.prototype.normalize =


Really bad. This is a major problem with Prototype and has burned them
when a browser implements the very same function. If you are keen on
namespacing then you definitely shouldn't be playing with common
objects.

"Don't modify objects you don't own."

Do this...

function normalizeString(string) {}

You are not making much sense here.

First of all, it is the ECMAScript implementation, not the browser, that
would implement this method. Therefore, there could only be two equally
probable outcomes: a) the implementation-defined property has the attribute
ReadOnly, and the implementation-defined property value is kept or b) the
implementation-defined property does not have the attribute ReadOnly, and
the implementation-defined property value is overwritten. With b) a more
efficient implement implementation would be overwritten by a less efficient
one. One can assume that both implementations would do what the method
identifier says, so the supposed damage would be negligible.

Second, I don't see anything wrong in augmenting String.prototype (or
Number.prototype, RegExp.prototype aso.), unless one would insist to use
for..in on string values (and numbers, or RegExp objects, respectively).
Your argument would be valid for Object.prototype, because of all native
objects inheriting from that and so with for..in iteration the user-defined
property would show up in a for..in loop with *any* parameter (as that is
converted to a corresponding object on for..in evaluation).


PointedEars
 
D

David Mark

You are not making much sense here.

First of all, it is the ECMAScript implementation, not the browser, that
would implement this method.  Therefore, there could only be two equally
probable outcomes: a) the implementation-defined property has the attribute
ReadOnly, and the implementation-defined property value is kept or b) the
implementation-defined property does not have the attribute ReadOnly, and
the implementation-defined property value is overwritten.  With b) a more
efficient implement implementation would be overwritten by a less efficient
one.  One can assume that both implementations would do what the method
identifier says, so the supposed damage would be negligible.

Second, I don't see anything wrong in augmenting String.prototype (or
Number.prototype, RegExp.prototype aso.), unless one would insist to use
for..in on string values (and numbers, or RegExp objects, respectively).
Your argument would be valid for Object.prototype, because of all native

I would add Array.prototype as well, at least if you want your code to
co-exist with other scripts, which may use for-in loops to iterate
through arrays (a bad idea of course, but often seen in practice.)

[snip]
 
D

dhtml

Sorry - I thought I clicked reply to author. I'm really not sleeping
enough these days.
Thanks for pointing this out. This is not the function I meant to
post. What I meant was the following, not to say that I haven't made a
mistake here.

function isHostMethod(object, property) {
return typeof object[property] == 'unknown' ||
// Would error for ActiveX but
// will have returned already.
object[property];

}
If it were to get to the second part,
object[prototype]
would cause an referenc error.

I don't think that is true. It is ok to access an undefined property
of an object. What is not ok is referencing a undeclared variable.

true

Garrett

Thanks again,
Peter
 
P

Peter Michaux

D

dhtml

On Feb 16, 2:19 pm, Peter Michaux wrote:
// version 3
function isHostMethod(object, property) {
return !!(typeof object[property]) ||
// For compliant objects which have a typeof
// result which is an empty string.
//
// Would error for ActiveX but
// will have returned already.
object[prototype];

isHostMethod(document, 'foo')
Is clearly broken.
It gets to
typeof object[property], that's a string.
!converts to false,
!! converts to true

No, the empty string has falseness (and is the only string primitive
value for which that is the case). The NOT operator converts the empty
string to boolean true, and double NOT will result in boolean false
when the subject is an empty string.
the method returns true. always.

<snip>

Not always.

I think somehow you missed the typeof operator when reading that.

!!(typeof object[property])

If typeof were to return an empty string, what you're saying could be
true. That would be a bug.

I'm not sure why when I click 'Reply to Author' that the reply gets
posted on the list. I know I've replied to posts indivually before and
they've been received.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top