Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript面向对象协助_Javascript_Client Side - Fatal编程技术网

javascript面向对象协助

javascript面向对象协助,javascript,client-side,Javascript,Client Side,我在javascript的oop方面遇到了问题。我有一个小的代码片段,它与我在项目中使用的代码类似。我写了一个小的代码示例,我也有这个问题。我想知道我的Javascript有什么问题,这样我的代码就可以工作了 function Person(name) { this.name = name; } Person.prototype.Display = {}; Display.prototype.text = function(str) { document.write(str + '&

我在javascript的oop方面遇到了问题。我有一个小的代码片段,它与我在项目中使用的代码类似。我写了一个小的代码示例,我也有这个问题。我想知道我的Javascript有什么问题,这样我的代码就可以工作了

function Person(name) {
  this.name = name;
}

Person.prototype.Display = {};

Display.prototype.text = function(str) {
  document.write(str + '<br />');
  window.alert(str);
};

var Jacob = new Person('Jacob');

Jacob.Display.text('Hello World!');
职能人员(姓名){
this.name=名称;
}
Person.prototype.Display={};
Display.prototype.text=函数(str){
编写(str+'
'); 窗口报警(str); }; var Jacob=新人(“Jacob”); Jacob.Display.text('helloworld!');
这个小代码示例应该显示文本helloworld。我遇到的问题是,
行“Jacob.Display.Text('Hello World!”);“
不起作用。

我想你的意思是:

function Person(name) {
  this.name = name;
}

Person.prototype.display = {
    text : function(str) {
        document.write(str + '<br />');
        window.alert(str);
    }
};

var Jacob = new Person('Jacob');
Jacob.display.text('Hello World!');
职能人员(姓名){
this.name=名称;
}
Person.prototype.display={
文本:函数(str){
编写(str+'
'); 窗口报警(str); } }; var Jacob=新人(“Jacob”); Jacob.display.text('helloworld!');
(还要注意“display”中的小写字母“d”;为构造函数保留大写首字母)

我想你的意思是:

function Person(name) {
  this.name = name;
}

Person.prototype.display = {
    text : function(str) {
        document.write(str + '<br />');
        window.alert(str);
    }
};

var Jacob = new Person('Jacob');
Jacob.display.text('Hello World!');
职能人员(姓名){
this.name=名称;
}
Person.prototype.display={
文本:函数(str){
编写(str+'
'); 窗口报警(str); } }; var Jacob=新人(“Jacob”); Jacob.display.text('helloworld!');

(还要注意“display”中的小写字母“d”;为构造函数保留大写首字母)

我不确定您到底想做什么,但这可能更接近您想要的

function Person(name) {
  this.name = name;
  this.element = document.createElement('div');
  document.body.appendChild(this.element);
}

Person.prototype.display = function(str) {
  this.element.textContent = str;
  console.log(this.name + ' set to ' + str);
};

var jacob = new Person('Jacob');

jacob.display('Hello World!');

我不确定你到底想做什么,但这可能更接近你想要的

function Person(name) {
  this.name = name;
  this.element = document.createElement('div');
  document.body.appendChild(this.element);
}

Person.prototype.display = function(str) {
  this.element.textContent = str;
  console.log(this.name + ' set to ' + str);
};

var jacob = new Person('Jacob');

jacob.display('Hello World!');

我猜想,
Display.prototype.text
会给出某种“未定义”或“不是函数”的异常情况。@RichardJPLeGuen是正确的。你的浏览器有一个JavaScript控制台;我建议查看出现的错误。另外,您不应该在页面加载后使用document.write!我使用JS控制台时出现了一个引用错误:未定义显示。我猜
Display.prototype.text
会给出某种类型的“未定义”或“不是函数”异常。@RichardJPLeGuen是正确的。你的浏览器有一个JavaScript控制台;我建议查看出现的错误。另外,您不应该在页面加载后使用document.write!我使用JS控制台并得到一个引用错误:未定义显示。如果我想从外部向显示对象添加属性和方法,代码是否为Display.prototype.*?没有
Display.prototype
,只有
Person.prototype.Display
。所以
Person.prototype.display.foo=function(){return'bar';}
。如果我想从外部向display对象添加属性和方法,代码会是display.prototype.*?没有
display.prototype
,只有
Person.prototype.display
。所以
Person.prototype.display.foo=function(){return'bar';}