This topic has been closed. No new entries allowed.
Reason: You can now post comments on articles on Dev Opera
You need to be logged in to post in the forums. If you do not have an account, please sign up first.
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
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com
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)
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:
Source: Mozilla Links - 5 things I’d like to see in Operawe will not be satisfied before we have the best developer tools in the industry
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
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
}
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;
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.
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.
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();
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();
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
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com
This turns addLink() into the more generic createLink()
But the example uses the former instead of the latter.
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
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com
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?
What is the difference in performance if you create and apply elements on demand rather than wait at the end of your generation process?