42: JavaScript best practices

Forums » Dev.Opera » Archived Article Discussions

This topic has been closed. No new entries allowed.

Reason: You can now post comments on articles on Dev Opera

Forum rules and guidelines

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

3. February 2009, 06:39:12

Opera Software

chrismills

Posts: 378

42: JavaScript best practices

In this article Christian Heilmann shares some JavaScript tips, tricks and best practices he has gleaned through painstaking hard work and experimentation in the field of JavaScript. These will help you make your code more efficient, more maintainable and more cross-platform compatible.

( Read the article )

Chris Mills
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com

3. February 2009, 10:22:58

dantesoft

No, really

Posts: 1248

Teaser!

6. March 2009, 19:45:21

fearphage

Trained Swordsman of Unwanted Opera Termination

Posts: 2231

Duck typing is passé. Here is a more robust solution.
Always latest weekly; XP Pro SP2
My bugs / disable RSS subscription prompt (This will disable email and chat as well) / Receive emailed copies of your bug reports

quote from desktopteam blog Feb 23 2007 06:49.36 (direct link to comment)

Originally posted by borg:

we will not be satisfied before we have the best developer tools in the industry

Source: Mozilla Links - 5 things I’d like to see in Opera

Originally posted by Percy Cabello:

One of the main reasons I prefer Firefox is that it starts from the belief that it can’t be the ideal browser for everybody

10. March 2009, 23:25:34

AmbroseChapel

Posts: 1

There are tons of mistakes in the code sections of this article, mostly missing brackets:

var myNameSpace = {
  current:null
  init:function){...},   // here
  change:function){...}, // here
  verify:function){...}  // and here
}

14. March 2009, 09:34:11

Aankhen

Posts: 1

I found the article quite useful, but there's something I'd like to point out. Under “shortcut notation” you say that this code:
var x = v || 10;
Will “automatically give `x' a value of `10' if `v' is not defined”. It will also default to `10' if `v' is `0' (which evaluates to `false' in a boolean context). If that is the desired behaviour, then no problem, but if you really wanted to check whether the variable is defined or not, use this:
var x = (v === undefined) ? 10 : v;

15. March 2009, 21:00:46

mrlami

Posts: 6

This does not have anything to do with this article, but where can one make suggestions to Opera about what they will like to see in the next versions of their browser?

I love this browser but as a web developer you cannot compare the tools available for FF to this. Opera does have better features but tools like dragonfly are just not up to par when you compare it to something like firebug.

Also it would be nice if one could arrange/categorize feeds into folders.

16. March 2009, 12:06:29

stelt

Posts: 59

typos:
thevolume
og

30. March 2009, 16:18:06

codebyjoe

Posts: 58

In the example below the class web_user extends the class user;

function user()
{
var that = {};
that.name = "";
that.address = "";
that.setName = function(name){that.name=name;};
that.setAddress = function(address){that.address=address;};
return( that );
};

function web_user()
{
var that = new user();
that.url = "";
that.setURL = function(url){that.url=url;};
return( that );
};

with this technique a class can also implement objects. in the example below a class is made emailable by impelementing the object emailable.

function emailable(that)
{
that.email = "";
that.getEmail = function(){return(that.email);};
}


function email_user()
{
var that = new web_user();
emailable(that);
return( that );
};

then i can create static elements for a class. in the example below users gets a static datum and method;

var staticUserData = {
userCount = 0;
};


function user()
{
var that = {};
that.name = "";
that.address = "";
that.setName = function(name){that.name=name;};
that.setAddress = function(address){that.address=address;};

// add the static data;
that.static = staticUserData;
// add a static method
if (that.static.incUserCount == undefined)
{
that.static.incUserCount = function(){that.static.userCount++;};
}

// show user added
that.static.incUserCount();
return( that );
};

and lastley i can protect methods that i don't want to be public. (sort of)
function user()
{
var that = {};
that.protected = {};
that.protected.doit = function(){alert("hello world");};
that.name = "";
that.address = "";
that.setName = function(name){that.name=name;};
that.setAddress = function(address){that.address=address;};

return( that );
};

var it = new user();
it.protected.doit();

6. April 2009, 23:13:56

Opera Software

chrismills

Posts: 378

Originally posted by stelt:

typos:thevolume
og



Fixed these - cheers.

Thanks for all the useful comments.

Chris Mills
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com

12. January 2010, 16:36:20

This turns addLink() into the more generic createLink()



But the example uses the former instead of the latter.

12. January 2010, 17:40:13

Opera Software

chrismills

Posts: 378

Originally posted by thierrykoblentz:

This turns addLink() into the more generic createLink()
But the example uses the former instead of the latter.



I'm not sure I get the problem here - the progression looks ok, and the example below this sentence uses createLink().
Chris Mills
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com

9. June 2011, 00:56:17

nson125

Posts: 1

In the section "Keep DOM access to a minimum" the author mentions that you should have a function that turns a string into DOM elements and call this function at the end of your generation process rather than constantly creating and applying elements.

What is the difference in performance if you create and apply elements on demand rather than wait at the end of your generation process?

Forums » Dev.Opera » Archived Article Discussions