SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prototype Question

    Is this possible?

    example 1
    Code:
    someObj = {top: function() { //some js code },bottom: function() { // some js code };
    myObj.prototype = someObj;
    myObj = {func1: function() { //some js code }, func2: function() { //some js code };
    For js to inherit another object using "prototype = object" does that object need to be created using the new keyword? (Check example 2)

    example 2
    Code:
    someObj = {
             top: function() { //some js code }, bottom: function() { // some js code }
     }
    
    
    function myObj() {
         this.func1 = function() { //some js code };
    
         this.func2 = function() { //some js code };
      }
    myObj.prototype = someObj;
    instance1 = new myObj();
    PHP MySQL Shopping Cart
    www.eprocart.com

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    myObj.prototype = new someObj();
    define myObj before assigning its prototype

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So for a new object to inherit the properties of an existing object the new object must be invoked using the "new" keyword?
    PHP MySQL Shopping Cart
    www.eprocart.com

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the new object's prototype must be an instance of the parent or super object.The you can add unique methods to extend it.

  5. #5
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if the new object is created as such?

    Code:
    myNewObj = {func: function() {//some code},func2: () {//some more code }};
    Can this object inherit the prototype of another object?

    example 2.
    Code:
    myNewObj.prototype = new oldObj();
    PHP MySQL Shopping Cart
    www.eprocart.com

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just starting to get my head round this, but the way I see it is:
    The prototype is just a collection of properties and methods, an object, that acts as a kind of group store for all objects created with a particular constructor function.
    When you try and use a property or method of an object, the object itself is checked first to see if it has that property or method, and if not then the object's prototype is checked.
    So, I think your example should work. myNewObj has its own methods and you've just changed its prototype property to point to another object. oldObj will now act as the secondary lookup for any properties or methods you try and use with myNewObj.
    I hope that's helpful (and true!)

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, now I don't think your example will work!

    prototypes are properties of constructor functions. They are not properties of object instances.

    If you use object literals, not constructor functions, then there will not be a prototype.

    function myOldObj() {} // constructor, so can be used with 'new'
    function myNewObj() {} // constructor, so has prototype
    myNewObj.prototype = new myOldObj();
    var instance = new myNewObj();
    // instance will now use prototype of myOldObj

  8. #8
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you create an instance of myObj, its constuctor will be returned as someObj.
    It will act just like one of the someObjs does, but it will also have access to methods and properties assigned only to the myObj prototype.

    It is a one way property lookup for the method-
    if a method is defined in the instance, use that,
    else if a method is defined in myObj, use that,
    else if it is defined in someObj, use that,
    else if it is an Object.prototype method, use that.


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
  •