如何在javascript对象中调用自原型函数构造函数[复制](How to call self prototype function in javascript object Constructor [duplicate])

这个问题在这里已有答案:

Javascript在构造函数 3中 调用原型函数的 答案

为什么我的人在调用一个自我方法时不说“你好”? 如何解决这个问题?

var Person = (function () { function Person() { this.prototype.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

This question already has an answer here:

Javascript calling prototype functions in constructor 3 answers

Why my person doesn't say "Hello" when calling one self method ? How to fix this?

var Person = (function () { function Person() { this.prototype.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

最满意答案

要在current对象上调用函数,不应该使用prototype ,只需调用它( this.say() )。

var Person = (function() { function Person() { this.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

要在javascript中了解有关OOP的更多信息,您可以阅读MDN上的文档

关于inheritance和prototype chain MDN上的文档和示例

来自@FelixKling的好消息

this引用的对象没有prototype属性。 只有函数具有原型属性。

To call function on the current object you shouldn't use prototype, just call it(this.say()).

var Person = (function() { function Person() { this.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

To learn more about OOP in javascript you can read docs on MDN

Nice doc and examples on MDN for inheritance and prototype chain

Good notice from @FelixKling

The object this refers to doesn't have a prototype property. Only functions have a prototype property.

如何在javascript对象中调用自原型函数构造函数[复制](How to call self prototype function in javascript object Constructor [duplicate])

这个问题在这里已有答案:

Javascript在构造函数 3中 调用原型函数的 答案

为什么我的人在调用一个自我方法时不说“你好”? 如何解决这个问题?

var Person = (function () { function Person() { this.prototype.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

This question already has an answer here:

Javascript calling prototype functions in constructor 3 answers

Why my person doesn't say "Hello" when calling one self method ? How to fix this?

var Person = (function () { function Person() { this.prototype.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

最满意答案

要在current对象上调用函数,不应该使用prototype ,只需调用它( this.say() )。

var Person = (function() { function Person() { this.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

要在javascript中了解有关OOP的更多信息,您可以阅读MDN上的文档

关于inheritance和prototype chain MDN上的文档和示例

来自@FelixKling的好消息

this引用的对象没有prototype属性。 只有函数具有原型属性。

To call function on the current object you shouldn't use prototype, just call it(this.say()).

var Person = (function() { function Person() { this.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person();

To learn more about OOP in javascript you can read docs on MDN

Nice doc and examples on MDN for inheritance and prototype chain

Good notice from @FelixKling

The object this refers to doesn't have a prototype property. Only functions have a prototype property.