PDA

View Full Version : object oriented programming with javascript



jigarshah
11-02-2006, 06:53 AM
can i simulate class in javascript ?
I also want to simulate pass by reference for javasrcipt. (I know its not possible to do it directly but some way to simulate)

codeexploiter
11-02-2006, 07:07 AM
Although JavaScript supports a data type we call an "object", the language's lack of strong typing and a formal inheritance mechanism mean that it is not a truly object-oriented language. Still, JavaScript does a good job of simulating the features of object-oriented languages like Java and C++. It is possible for create classes in Javascript. Checkout this one (http://www.microcyb.com/?m=c&c=333)

I don't think Javascript supports parameter passing by reference at least in case of primitive (scalar variables)

blm126
11-02-2006, 12:18 PM
It is possible for create classes in Javascript.
No it's not. Javascripts OO features are prototype based, and not class based.

mwinter
11-02-2006, 04:07 PM
can i simulate class in javascript ?

That depends on what aspects of class-based languages you want to simulate.



I also want to simulate pass by reference for javasrcipt.

All objects are passed by reference. Many built-in objects are immutable, but added properties or modified values will persist outside of a function:



var object = { member : 'value' };

function myFunction(argument) {
argument.member = 'new value';
}

alert(object.member); // value
myFunction(object);
alert(object.member); // new value





Although JavaScript supports a data type we call an "object", the language's lack of strong typing and a formal inheritance mechanism mean that it is not a truly object-oriented language.

Strong typing isn't a requirement of object-oriented languages, and ECMAScript derivatives do have an inheritance mechanism. The fact that the latter is prototype-based rather than class-based is what makes it object-based rather than object-oriented.



Still, JavaScript does a good job of simulating the features of object-oriented languages like Java and C++.

It doesn't simulate the other features of object-oriented language; it has them.



It is possible for create classes in Javascript.

As Brady wrote, it is not. There are no classes, only prototypes.



Checkout this one (http://www.microcyb.com/?m=c&c=333)

That is very badly written, misleading, and the code snippets blatantly do not work as intended.



I don't think Javascript supports parameter passing by reference at least in case of primitive (scalar variables)

Primitives (strings, numbers, booleans, null, undefined) are indeed passed by value.

Mike

Twey
11-02-2006, 06:18 PM
Objects are always passed by reference; primitives are always passed by value. However, it's possible to pass an object and a property name:
function changeString(obj, prop) {
obj[prop] = "bar";
}

var s = { 'stringValue' : "foo" };
changeString(s, "stringValue");
// s.stringValue is now "bar"... or to copy an object:
function byVal(obj) {
var t = {};
if(obj.prototype)
t.prototype = obj.prototype;
for(var i in obj)
t[i] = obj[i];
return t;
}

function changeString(obj, prop) {
obj[prop] = "bar";
}

var s = { 'stringValue' : "foo" };
changeString(byVal(s), "stringValue");
// s.stringValue is still "foo"... although there are likely pitfalls to the latter, especially in advanced usage (only Enumerable properties will be passed by the loop; others must be specified manually; Immutable properties can't be changed, so if the new object has one by default [like the constructor property] it can't be overwritten).

mwinter
11-03-2006, 02:19 AM
function byVal(obj) {
var t = {};
if(obj.prototype)
t.prototype = obj.prototype;
for(var i in obj)
t[i] = obj[i];
return t;
}

The utility of that is really quite limited, as you suspected. For example, if state is maintained internally, either as [[Value]] (built-in and host objects) or using closures, it cannot be copied in this way.

The prototype property is only a property of function objects, and copying it in this case doesn't really make much sense as the returned object will not be a function.



Immutable properties can't be changed, so if the new object has one by default [like the constructor property] it can't be overwritten).

Yes, though for the record, the constructor property shouldn't be read-only. It is a property of prototype objects, so its value can either be change there, or by creating the property on an inheriting object.

Mike