Results 1 to 6 of 6

Thread: object oriented programming with javascript

  1. #1
    Join Date
    Oct 2006
    Posts
    33
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default object oriented programming with javascript

    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)

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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)

  3. #3
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by codeexploiter
    It is possible for create classes in Javascript.
    No it's not. Javascripts OO features are prototype based, and not class based.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jigarshah
    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:

    Code:
    var object = { member : 'value' };
    
    function myFunction(argument) {
        argument.member = 'new value';
    }
    
    alert(object.member);  // value
    myFunction(object);
    alert(object.member);  // new value

    Quote Originally Posted by codeexploiter
    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
    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

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Objects are always passed by reference; primitives are always passed by value. However, it's possible to pass an object and a property name:
    Code:
    function changeString(obj, prop) {
      obj[prop] = "bar";
    }
    
    var s = { 'stringValue' : "foo" };
    changeString(s, "stringValue");
    // s.stringValue is now "bar"
    ... or to copy an object:
    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"
    ... 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).
    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!

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Code:
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •