返回为未定义的Javascript对象方法

返回为未定义的Javascript对象方法,javascript,function,object,Javascript,Function,Object,我已经为这段代码挣扎了好几天了。本质上,它是一个对象,接收3个文本框的输入,并将它们连接成一个字符串。。或者这就是它应该做的 toString返回为未定义或“未捕获类型错误:对象[object]的属性'Student'不是函数”,当它应该是姓、名,然后是id号时。谁能解释一下我在这件事上做错了什么 <div id='textInside'> First Name:<input type='text' id='fName'><br> La

我已经为这段代码挣扎了好几天了。本质上,它是一个对象,接收3个文本框的输入,并将它们连接成一个字符串。。或者这就是它应该做的

toString返回为未定义或“未捕获类型错误:对象[object]的属性'Student'不是函数”,当它应该是姓、名,然后是id号时。谁能解释一下我在这件事上做错了什么

    <div id='textInside'>
    First Name:<input type='text' id='fName'><br>
    Last Name:<input type='text' id='lName'><br>
    Student Id: <input type='text' id='stuId'><br>
    <button onclick='readInputs()'>Update</button>

    <p id='stuInfo'></p>
</div>

<script type='text/javascript'>
    var fiName = document.getElementById('fName');
    var laName = document.getElementById('lName');
    var studeId = document.getElementById('stuId');
    var toP = document.getElementById('stuInfo');

    var Student = new Object;

    function Student() {
        this.firstName = fiName;
        this.lastName = laName;
        this.studentId = studeId;
        this.toString = function() {var result = toP.innerHTML = this.lastName + this.firstName + this.studentId};
            return result;
        this.readInputs = function() { };
    }



    function readInputs() {
        Student();
        Student.toString();
    }
</script>

名字:
姓氏:
学生Id:
更新

var fiName=document.getElementById('fName'); var laName=document.getElementById('lName'); var studeId=document.getElementById('stuId'); var toP=document.getElementById('stuInfo'); var Student=新对象; 函数学生(){ this.firstName=fiName; this.lastName=laName; this.studentId=studeId; this.toString=function(){var result=toP.innerHTML=this.lastName+this.firstName+this.studentId}; 返回结果; this.readInputs=函数(){}; } 函数readInputs(){ 学生(); Student.toString(); }
尝试更改此选项

 var student = new Student(fiName,laName,studeId);
 var res = student.toString();

 function Student(fiName,laName,studeId) {
        this.firstName = fiName;
        this.lastName = laName;
        this.studentId = studeId;
    }

   Student.prototype.toString = function() {
        return this.lastName + this.firstName + this.studentId
    }
上面是一个面向对象的java脚本代码示例。我已经在新的Student类型上定义了一个Student构造函数和一个toString方法。
建议将方法添加到原型链中,以便将它们绑定到类型,而不仅仅是特定实例

要在对象上实例化,必须使用单词new

由于生成了student,因此必须将其作为参数提供给构造函数

代码应该是这样的

<div id='textInside'>
    First Name:<input type='text' id='fName'><br>
    Last Name:<input type='text' id='lName'><br>
    Student Id: <input type='text' id='stuId'><br>
    <button onclick='readInputs()'>Update</button>

    <p id='stuInfo'></p>
</div>

<script type='text/javascript'>
var Student = function( option ) {
    this.firstName = option.fiName;
    this.lastName = option.laName;
    this.studentId = option.studeId;
    this.top = option.top;
};

Student.prototype.toString = function(){
     return document.write(this.lastName +" "+ this.fastName +" "+ this.studentId);
};

function readInputs() {
    var student = new Student({
         fiName: document.getElementById('fName').value.trim(),
         laName: document.getElementById('lName').value.trim(),
         studeId: document.getElementById('stuId').value.trim(),
         top: document.getElementById('stuInfo').value.trim()
    });
    student.toString();
}
</script>

名字:
姓氏:
学生Id:
更新

var Student=函数(选项){ this.firstName=option.fiName; this.lastName=option.laName; this.studentId=option.studeId; this.top=option.top; }; Student.prototype.toString=函数(){ 返回文档。写入(this.lastName+“”+this.fastName+“”+this.studentId); }; 函数readInputs(){ 学生=新生({ fiName:document.getElementById('fName').value.trim(), laName:document.getElementById('lName').value.trim(), studeId:document.getElementById('stuId').value.trim(), 顶部:document.getElementById('stuInfo').value.trim() }); student.toString(); }

这里有很多错误

var Student = new Object;

function Student() {
    this.firstName = fiName;
    this.lastName = laName;
    this.studentId = studeId;
    this.toString = function() {var result = toP.innerHTML = this.lastName + this.firstName + this.studentId};
        return result;
    this.readInputs = function() { };
}
首先,使用
{}
,而不是
新对象
。接下来,这将只将
Student
设置为
新对象
,并覆盖您的函数,因为函数已被提升。我很确定你只是想在那里有一个功能

接下来,
返回结果
不在
toString
函数中,这是一个错误,并且不是您要报告的错误。这实际上是你的代码吗?也使用原型。而
toString
不应该有副作用,构造函数也不应该读取全局变量

function Student(firstName, lastName, studentId) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.studentId = studentId;
}

Student.prototype.readInputs = function() {};

Student.prototype.toString = function() {
    return this.lastName + this.firstName + this.studentId;
};
现在,
readInputs
看起来是这样的(顺便问一下,这与
Student
s上的空函数有关吗?)

请注意,您必须对构造函数使用
new
来正确创建
this
,并获取输入元素的
value
属性,并实际处理函数的返回值

此错误:

未捕获的TypeError:对象[object object]的属性“Student”不是函数

对我来说似乎不可能,但我可能只是错过了一些东西。(你在IE8上吗?


名字:
姓氏:
学生Id:
更新

var fiName=document.getElementById('fName'); var laName=document.getElementById('lName'); var studeId=document.getElementById('stuId'); //定义在哪里?。。 var toP=document.getElementById('stuInfo'); 函数学生(){ this.firstName=fiName.value; this.lastName=laName.value; this.studentId=studeId.value; this.toString=函数(){ var result=toP.innerHTML+this.lastName +this.firstName+this.studentId 返回结果; }; } 函数readInputs(){ var a_student=新学生(); 警惕(一个学生.toString()); }
您应该在获取元素id后输入.value。因为您可以获取元素id,但不能获取其值

var fiName = document.getElementById('fName').value;

这使得变量有一个值

下一次,将您的示例放在JSFIDLE中,您将使我们都更加高兴。你有两个学生。而且你没有使用新的,麻烦了。您的
toString
需要一个实例。您发布的代码有几个问题。也许最好弄清楚你想做什么。@DanFarrell不,不会的。最好在这里发布简洁的代码,因为这样更方便,并且可以将代码与问题关联起来,而不是抽象到其他网站。这两者并不相互排斥。这是一个好的开始,但如果您描述了您的答案并删除
var fiName=新对象
不是一个函数,在
Student()
@elclanrs:是的,我只是想知道“of[Object]”是从哪里来的。我猜是某个浏览器的某个版本。我明白了,是的,我会选择IE,指责IE似乎是对的。@elclanrs责怪IE干什么?错误是返回对象的
[[Class]]
,返回的方式与typeof返回的方式大致相同,并表示“此对象是类对象,不实现调用,因此它不是函数”。我可不喜欢你,b
<div id='textInside'>
First Name:<input type='text' id='fName'><br>
Last Name:<input type='text' id='lName'><br>
Student Id: <input type='text' id='stuId'><br>
<button onclick='readInputs()'>Update</button>

<p id='stuInfo'></p>
</div>

<script type='text/javascript'>
    var fiName = document.getElementById('fName');
    var laName = document.getElementById('lName');
    var studeId = document.getElementById('stuId');
    // Where is <node id="stuInfo" /> defined?..
    var toP = document.getElementById('stuInfo');

    function Student() {
        this.firstName = fiName.value;
        this.lastName = laName.value;
        this.studentId = studeId.value;
        this.toString = function() {
            var result = toP.innerHTML + this.lastName 
                + this.firstName + this.studentId
            return result;
            };
    }

    function readInputs() {
        var a_student = new Student();
        alert(a_student.toString());
    }
</script>
var fiName = document.getElementById('fName').value;