Dev.Opera - Follow the standards, break the rulesDev.Opera - Follow the standards, break the rules

Login

Lost password?

Forums » Article Discussions

Discuss the articles posted on Dev.Opera.

Note: You need to login to post in the forums. if you don't have an account you first need to sign up.

By chrismills O anchor Tuesday, 3. February 2009, 06:39:12

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 )

By dantesoft anchor Tuesday, 3. February 2009, 10:22:58

avatarTeaser!

By fearphage anchor Friday, 6. March 2009, 19:45:21

avatarDuck typing is passé. Here is a more robust solution.

By AmbroseChapel anchor Tuesday, 10. March 2009, 23:25:34

avatarThere 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
}

By Aankhen anchor Saturday, 14. March 2009, 09:34:11

avatarI 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;

By mrlami anchor Sunday, 15. March 2009, 21:00:46

avatarThis 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.

By stelt anchor Monday, 16. March 2009, 12:06:29

avatartypos:
thevolume
og

By codebyjoe anchor Monday, 30. March 2009, 16:18:06

avatarIn 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();

By chrismills O anchor Monday, 6. April 2009, 23:13:56

avatar

Originally posted by stelt:

typos:thevolume
og



Fixed these - cheers.

Thanks for all the useful comments.

Moderators: pepelsbey | dstorey | mcx | operadev | chrismills | shwetankdixit | brucelawson | iheni | andreasbovens | zibin | mollydotcom