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)
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)
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
I don't think Javascript supports parameter passing by reference at least in case of primitive (scalar variables)
No it's not. Javascripts OO features are prototype based, and not class based.Originally Posted by codeexploiter
That depends on what aspects of class-based languages you want to simulate.Originally Posted by jigarshah
All objects are passed by reference. Many built-in objects are immutable, but added properties or modified values will persist outside of a function:I also want to simulate pass by reference for javasrcipt.
Code:var object = { member : 'value' }; function myFunction(argument) { argument.member = 'new value'; } alert(object.member); // value myFunction(object); alert(object.member); // new value
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.Originally Posted by codeexploiter
It doesn't simulate the other features of object-oriented language; it has them.Still, JavaScript does a good job of simulating the features of object-oriented languages like Java and C++.
As Brady wrote, it is not. There are no classes, only prototypes.It is possible for create classes in Javascript.
That is very badly written, misleading, and the code snippets blatantly do not work as intended.Checkout this one
Primitives (strings, numbers, booleans, null, undefined) are indeed passed by value.I don't think Javascript supports parameter passing by reference at least in case of primitive (scalar variables)
Mike
Objects are always passed by reference; primitives are always passed by value. However, it's possible to pass an object and a property name:... or to copy an object:Code:function changeString(obj, prop) { obj[prop] = "bar"; } var s = { 'stringValue' : "foo" }; changeString(s, "stringValue"); // s.stringValue is now "bar"... 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).Code: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"
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
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.Originally Posted by Twey
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.
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.Immutable properties can't be changed, so if the new object has one by default [like the constructor property] it can't be overwritten).
Mike
Bookmarks