JavaScript Object-Oriented Programming Part 1

This is a dedicated thread for discussing the SitePoint article ‘JavaScript Object-Oriented Programming Part 1

Wonderful!!

Thank you for this very good and well explained lesson.

I’m a beginer in Web programming and I have worked with Java.

It is a good brief introduction to Javascript object oriented programming. thank you

It is very clear explaination. I can’t wait for part 2.

do you have a “total tutorial” out somewhere? I enjoyed this one I wish i had you as an instructor!

wow, create objects in JS, who knew. Very good introduction!!!

Very well explained! Thanks a lot.

I have been programming with JavaScript for a long time and am pretty-much my office’s “resident expert” on the subject. I knew about JavaScript’s object-oriented-like abilities, but the need hadn’t arisen for them before. Suddenly I was faced with a challenge that seemed to call for just that and I found this article. It helped me tremendously. Thanks!

Good one. Very clear.

In JavaScript, there are five primitive data types: Undefined, Null, Boolean, Number, and String.

In JavaScript, everything is an object, except for the primitive data types.


var s1 = "test1";
var s2 = String("test2");
var s3 = new String("test3");

function test(s) {
    s = "asd";
}

test(s1);
alert(s1);

test(s2);
alert(s2);

test(s3);
alert(s3);

alert(s1.length);
alert(s2.length);
alert(s3.length);

Primitive types are not objects and cannot have properties, so what is “s1.length” ?
Objects are passed by reference and primitive data types by value. But in this example you can see that an object s3 is passed by a value to function test().
So what is the difference between s1, s2 and s3 ?
Is this article outdated ?

http://www.chinalinuxpub.com/doc/oreillybookself/web/jscript/ch03_11.htm

http://www.planetpdf.com/mainpage.asp?webpageid=1144

cagrET,

THe last link adios left gives a good explanation.

Strings, as we know, are primitives. However, when we use them we usually want to have the .lenth property and all the String methods, so JavaScript promotes them to objects.

JavaScript sometimes does such conversions. For example, if you put something like:

if(new Object())

It converts that into a boolean. In a similar way, JavaScript often promotes String’s between strings and String objects.

Hope that clarifies some…more detail could be gone into about it, and I think the last link does a decent job.

aDog :cool:

What a great article. I have been using this sort of approach with ActionScript and hadn’t yet tried it with JS. Very helpful in getting started. Thanks.

if i have an array of objects, how do i sort it by member function ?

Using what criteria? :confused:

http://www.scottandrew.com/main/entries/000179

Nice one. You exposed enough parallels between JS and general OO concepts that I understood the JS Object concept easily.

You may find that by emphasising the importance the “this” variable, you will provide greater benefit to JSers who haven’t tackled OO before.

Thankyou very much, this short tutorial cleared up a lot of confusion I had re javascript objects and how to customise them.

amazing.
short / clear / concise / effective.
this guy is a master.

Just what I was searching for :slight_smile: