Comments on: JavaScript, We Hardly new Ya http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/ News and Artilces about Designing and Developing with Yahoo! Libraries. Mon, 12 May 2008 04:52:09 +0000 http://wordpress.org/?v=2.3.3 By: GANCHIKU.com » JavaScript Module Patternいいね。半年遅れだけど。。。 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-232489 GANCHIKU.com » JavaScript Module Patternいいね。半年遅れだけど。。。 Sat, 17 Nov 2007 14:36:49 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-232489 [...] JavaScript, We Hardly new Ya [...] […] JavaScript, We Hardly new Ya […]

]]>
By: Jay http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-155237 Jay Tue, 10 Jul 2007 14:29:08 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-155237 Dear Scott, <code> new function(...) { ... } </code> is (mostly) equivalent to <code> (function(...) { ... }) () </code> (The function is defined and evaluated in-place, oftentimes to create private, closed-over data, and oftentimes to do a switch against the current browser or some other environment feature to determine what function you actually want to use later on, instead of constantly testing the environment every time you call that function.) Evaluation in-place using parentheses is more efficient, but syntactically ugly, and it's not clear until the very end that it's being evaluated in-place. Using the new keyword is less efficient and less immediately obvious unless you're familiar with the style, but its syntax is nicer and, assuming you've seen the style before, it's clear up-front that the function is being defined and evaluated in-place. Dear Scott,


new function(...) { ... }

is (mostly) equivalent to


(function(...) { ... }) ()

(The function is defined and evaluated in-place, oftentimes to create private, closed-over data, and oftentimes to do a switch against the current browser or some other environment feature to determine what function you actually want to use later on, instead of constantly testing the environment every time you call that function.)

Evaluation in-place using parentheses is more efficient, but syntactically ugly, and it’s not clear until the very end that it’s being evaluated in-place. Using the new keyword is less efficient and less immediately obvious unless you’re familiar with the style, but its syntax is nicer and, assuming you’ve seen the style before, it’s clear up-front that the function is being defined and evaluated in-place.

]]>
By: Renzo Kooi http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-136094 Renzo Kooi Tue, 05 Jun 2007 20:56:09 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-136094 In addition to my last comment, I carried it one step further and augmented Object like this: <code> <strong>Object.prototype.toObject</strong> = function() { var object = function(o) { function F(){} F.prototype = o; return F.prototype; } return object(this); }</code> So now I am able to rewrite someConstructor like this: <code> function someConstructor() { [code] return <strong>this.toObject()</strong>; }</code> Worked in my code, but not sure if this could have unforeseen consequences. Anyone has an idea about that? In addition to my last comment, I carried it one step further and augmented Object like this:

Object.prototype.toObject = function() {
var object = function(o) {
function F(){}
F.prototype = o;
return F.prototype;
}
return object(this);
}

So now I am able to rewrite someConstructor like this:

function someConstructor() {
[code]
return this.toObject();
}

Worked in my code, but not sure if this could have unforeseen consequences. Anyone has an idea about that?

]]>
By: Renzo http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-134768 Renzo Sun, 03 Jun 2007 10:00:07 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-134768 A bit off topic perhaps, but may be interesting. I slightly modified Douglas Crockfords 'object' function to this: <code> function object(o) { function F(){} F.prototype = o; <em>return F.prototype;</em> }</code> Now I am able to do this: <code> function someConstructor() { [code] <em>return object(this);</em> }</code> And point to someConstructor without using 'new' or 'object', like this: <code> var myObject = someConstructor();</code> See an (fairly useless) example @ <a href="http://www.nicon.nl/nohtml2/nohtmlx.html" title="example" rel="nofollow">My website</a> A bit off topic perhaps, but may be interesting. I slightly modified Douglas Crockfords ‘object’ function to this:

function object(o) {
function F(){}
F.prototype = o;
return F.prototype;
}

Now I am able to do this:

function someConstructor() {
[code]
return object(this);
}

And point to someConstructor without using ‘new’ or ‘object’, like this:

var myObject = someConstructor();

See an (fairly useless) example @ My website

]]>
By: Renzo Kooi http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-134756 Renzo Kooi Sun, 03 Jun 2007 09:24:13 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-134756 For all I know there's no need to use 'new' for new functions. Just 'Function' will do. I use it to convert a function string (for example from a JSON-like string) to a real function. Like this: var freshFunction = function(args,body) { if (args.length>0) { var sArgs = args.join(','); } else { var sArgs = ''; } return Function(sArgs,body); } var fnFromString = function(fnStr) { var f1 = f2 = fnStr.replace(/\n|\r/g,''), f1a = (f1.split(/\{/i))[0], f1b = (f1a.split(/\(/i))[1].replace(/\)/,'').trim(); if (f1b.length>0){ var fx = f1b.split(','); } else { var fx = []; } // get function body f2 = f2.replace(/(^function.+\{)(.+)(\}$)/i,"$2"); return freshFunction(fx,f2); } (sorry, perhaps this is posted twice: I am not sure the post came trough, firefox told me the connection was reset after clicking the submit button.) For all I know there’s no need to use ‘new’ for new functions. Just ‘Function’ will do. I use it to convert a function string (for example from a JSON-like string) to a real function. Like this:

var freshFunction = function(args,body) {
if (args.length>0) {
var sArgs = args.join(’,');
} else {
var sArgs = ”;
}
return Function(sArgs,body);
}

var fnFromString = function(fnStr) {
var f1 = f2 = fnStr.replace(/\n|\r/g,”),
f1a = (f1.split(/\{/i))[0],
f1b = (f1a.split(/\(/i))[1].replace(/\)/,”).trim();
if (f1b.length>0){
var fx = f1b.split(’,');
} else {
var fx = [];
}
// get function body
f2 = f2.replace(/(^function.+\{)(.+)(\}$)/i,”$2″);
return freshFunction(fx,f2);
}

(sorry, perhaps this is posted twice: I am not sure the post came trough, firefox told me the connection was reset after clicking the submit button.)

]]>
By: Shall We Zen » links for 2007-05-16 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-116592 Shall We Zen » links for 2007-05-16 Wed, 16 May 2007 20:32:03 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-116592 [...] JavaScript, We Hardly new Ya » Yahoo! User Interface Blog (tags: javascript optimization performance) [...] […] JavaScript, We Hardly new Ya » Yahoo! User Interface Blog (tags: javascript optimization performance) […]

]]>
By: Darryl Stoflet http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-94635 Darryl Stoflet Thu, 19 Apr 2007 18:59:24 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-94635 Good stuff! I also just watched the Advanced JavaScript videos on <a href="http://rds.yahoo.com/_ylt=A0WTQ0QVtCdGNhABGFUBP88F/SIG=123ao8iag/EXP=1177093525/**http%3A//video.yahoo.com/video/group%3Fgid=133414" rel="nofollow"> YUI Theater</a> and feel this is a lot of great info to absorb. Couple of questions tho... Firstly, without new (as you state) you are giving up the prototype object. But I thought one very real benefit of the prototype object is to assign methods to them once and have them shared by all, thus saving space. It seems your recommended obj literal notation requires the repeated inclusion of inline functions for every object created. I presume you consider this an acceptable trade off? Secondly, how do you represent static variable/methods (public or private) without the use of new? Good stuff! I also just watched the Advanced JavaScript videos on
YUI Theater
and feel this is a lot of great info
to absorb.

Couple of questions tho…
Firstly, without new (as you state) you are giving up the prototype object. But I thought one very real benefit of the prototype object is to assign methods to them once and have them shared by all, thus saving space. It seems your recommended obj literal notation requires the repeated inclusion of inline functions for every object created. I presume you consider this an acceptable trade off?

Secondly, how do you represent static variable/methods (public or private) without the use of new?

]]>
By: Confluence: Ask Site Engineering http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-70847 Confluence: Ask Site Engineering Tue, 20 Mar 2007 14:02:14 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-70847 <strong>Javascript Best Practices...</strong> A tentative repository of JavaScript bestpractices with examples (WORK IN PROGRESS). @TODO: Cleanup and reorder  1. Avoid global functions or variables It is easy to forget how/where these are/were instantiated....... Javascript Best Practices…

A tentative repository of JavaScript bestpractices with examples (WORK IN PROGRESS). @TODO: Cleanup and reorder  1. Avoid global functions or variables It is easy to forget how/where these are/were instantiated…….

]]>
By: Theodor Zoulias http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-49563 Theodor Zoulias Fri, 16 Feb 2007 19:41:40 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-49563 ;(sometest() ? command1 : command2)(arg1, arg2, arg3) This coding style makes semicolons omission no longer an option. ;(sometest() ? command1 : command2)(arg1, arg2, arg3)

This coding style makes semicolons omission no longer an option.

]]>
By: liorean http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-31927 liorean Sun, 14 Jan 2007 03:21:43 +0000 http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/#comment-31927 Well, you definitely have a point with array and object initialisers or literals for primitive types, but there is a quite strong argument in favour of <code>new Function</code>, I think. The reason is: function declarations and function initialisers are locally scoped, in other words create closures. But <code>new Function</code> allows the creation of a globally scoped function object in a nested scope, so no closure of the current scope is created. Practical if you want to get around some common memory leaks. Also, <code>new Function</code> can be used for example as an alternative to <code>eval</code> if you want to emulate <code>Function.prototype.apply </code> in ie5. Well, you definitely have a point with array and object initialisers or literals for primitive types, but there is a quite strong argument in favour of new Function, I think. The reason is: function declarations and function initialisers are locally scoped, in other words create closures. But new Function allows the creation of a globally scoped function object in a nested scope, so no closure of the current scope is created. Practical if you want to get around some common memory leaks.

Also, new Function can be used for example as an alternative to eval if you want to emulate Function.prototype.apply in ie5.

]]>