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();
asked Aug 17, 2014 at 18:31
5
  • 4
    var user2 = Object.create(user) Commented Aug 17, 2014 at 18:32
  • 1
    prototype properties are associated with constructor functions for use when creating new instances, not with the instances themselves. Commented Aug 17, 2014 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 ^^^ Commented Aug 17, 2014 at 18:36
  • stackoverflow.com/questions/16063394/… Commented Aug 18, 2014 at 2:11
  • possible duplicate of JavaScript Object Literals & Array Literals Commented Aug 25, 2014 at 7:25

2 Answers 2

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();
answered Aug 17, 2014 at 18:45
Sign up to request clarification or add additional context in comments.

Comments

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;
 }
 }
});
answered Aug 17, 2014 at 18:40

2 Comments

@Viktorino if you just starts with prototypes I thought that this will be helpful :)
thank's :) I have a little dark in the eyes, but in general it's OK)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.