0

i am just starting learn a prototypes in javascript an i can't understand what a problem is in my code. I'm sorry, my question may seem silly

i got an error like this : Uncaught TypeError: undefined is not a function

why is undefined? I inherited a function of the user. can't understand it :(

var user = {

    sayName: function() {
        console.log(this.name)
    }

  };

user.name = "viktor";

user.sayName();

var user2 = {};

user2.prototype = user;

user2.name = "segey";

user2.sayName();
Victorino
  • 1,623
  • 11
  • 22
  • 4
    `var user2 = Object.create(user)` – elclanrs Aug 17 '14 at 18:32
  • 1
    `prototype` properties are associated with constructor `function`s for use when creating `new` instances, not with the instances themselves. – Jonathan Lonowski Aug 17 '14 at 18:35
  • The `.prototype` property is only really relevant on a function. The function, when invoked using `new` establishes the relationship between the new object being created and the function's `.prototype` object. *...like Jonanthan said ^^^* – cookie monster Aug 17 '14 at 18:36
  • http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Aug 18 '14 at 02:11
  • possible duplicate of [JavaScript Object Literals & Array Literals](http://stackoverflow.com/questions/500583/javascript-object-literals-array-literals) – Victorino Aug 25 '14 at 07:25

2 Answers2

2

All you need to set up the prototype chain with plain objects is:

var user2 = Object.create(user); // set `user` as prototype of `user2`

user2.name = "segey";
user2.sayName();
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

For you question correct solution will:

function User() {
    this.name = 'Viktor';
    return this;
}

User.prototype = Object.create({
    sayName: function() {
        return this.name;
    }
});

function User2() {}
User2.prototype = Object.create(User.prototype);

var user = new User();
user.sayName(); // 'Viktor'
user2 = new User2();
user2.name = 'Bogdan';
user2.sayName(); // 'Bogdan'

And detailed explanation with example. Let say we have some basic class Animal. Our Animal have age and name.

function Animal() {
    this.age = 5;
    this.name = "Stuffy";
    return this;
}

Animal.prototype = Object.create({
    getAge: function() {
        return this.age;
    },

    getName: function() {
        return this.name;
    }
});

And when I spent some time with architecture I understand that I also need SubClass of Animal. For example, let it will be Dog class with new property and functions. And also Dog must extend functions and properties from Animal class.

function Dog() {
    Animal.apply(this, arguments); // Call parent constructor
    this.wantsEat = true; // Add new properties
    return this;
}

Dog.prototype = Object.create(Animal.prototype); // Create object with Animal prototype
Object.extend(Dog.prototype, { // Any extend() function, wish you want
    constructor: Dog, // Restore constructor for Dog class
    eat: function() {
        this.wantsEat = false;
        return this;
    }
});

Or you can use Object.defineProperties() and extend in this way:

Dog.prototype = Object.create(Animal.prototype, {
    constructor: {
        value: Dog
    },

    eat: {
        value: function() {
            this.wantsEat = false;
            return this;
        }
    }
});
Eugene Obrezkov
  • 2,910
  • 2
  • 17
  • 34