最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript prototype method "Cannot set property" - Stack Overflow

programmeradmin2浏览0评论

I'm always getting Cannot set property 'saySomething' of undefined but why? Am I making a mistake somewhere?

var Person = new Object();

Person.prototype.saySomething = function ()
{ 
  console.log("hello"); 
};

Person.saySomething();

I'm always getting Cannot set property 'saySomething' of undefined but why? Am I making a mistake somewhere?

var Person = new Object();

Person.prototype.saySomething = function ()
{ 
  console.log("hello"); 
};

Person.saySomething();
Share Improve this question edited Dec 7, 2018 at 14:24 nem035 35.5k6 gold badges92 silver badges104 bronze badges asked Jan 23, 2017 at 0:17 SamySamy 1,0333 gold badges16 silver badges29 bronze badges 1
  • Why are you trying to use prototype? You got no class, you got only one object, so just assign the method to it directly: Person.saySomething = … – Bergi Commented Jan 23, 2017 at 0:29
Add a comment  | 

5 Answers 5

Reset to default 10

Debugging tip: You get this ..of undefined errors when you try to access some property of undefined.

When you do new Object(), it creates a new empty object which doesn't have a prototype property.

I am not sure what exactly are we trying to achieve here but you can access prototype of function and use it.

var Person = function() {};

Person.prototype.saySomething = function() {
  console.log("hello");
};

var aperson = new Person();
aperson.saySomething();

The prototype property exists on functions, not on instantiated objects.

var Person = new Object();
console.log(Person.prototype); // undefined

var Person2 = function () {}
console.log(Person2.prototype); // {}

This is useful because things put on the prototype of a function will be shared by all object instances created with that function (by using new).

var Person = function() {};

Person.prototype.saySomething = function() {
  console.log("hello");
};

console.log(
  new Person().saySomething === Person.prototype.saySomething // true. they are the same function
);

If all you want is to add a method to the person object, there's no need for a prototype:

var Person = {};

Person.saySomething = function() {
  console.log("hello");
};

Person.saySomething();

You can even use object literal syntax:

var Person = {
  saySomething: function() {
    console.log("hello"); 
  }
};

Person.saySomething();

i was trying out some code thought of posting it, might help others.

<script>

        var MODULE = {};
        MODULE = (function (my) {
            my.anotherMethod = function () {
                console.log("hello ");
            };

            my.newMethod = function(){
                console.log("hi new method ");

            }
            return my;
        }(MODULE));


        MODULE.anotherMethod(); 
        MODULE.newMethod(); 


    </script>

And please not var MODULE ={}, if this is not initialized with {} then it give cannot set property.

I know i am late to the party but as you see there is no satisfying answer available to the question so i am providing my own.

In your case when you write

var Person = new Object();

you are creating an instance of Object type. You can add a property using prototype property to the Object, not to the instance of Object.which you can use by the instance laterly.

so you can define like

Object.prototype.saySomething = function ()
{ 
  console.log("hello"); 
};

now you can call it like this.

Person.saySomething();

You can check here.

var Person = function(name) {
  this.canTalk = true;
  this.name = name;
};
Person.prototype.greet = function() {
  if (this.canTalk) {
    console.log('Hi, I am ' + this.name);
  }
};

bob = new Person('bob');
bob.greet();
发布评论

评论列表(0)

  1. 暂无评论