Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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_Prototype_Javascript Objects - Fatal编程技术网

JavaScript两个对象-相互重写

JavaScript两个对象-相互重写,javascript,prototype,javascript-objects,Javascript,Prototype,Javascript Objects,我正在努力学习JavaScript中的面向对象编程。因此,我所做的可能是错误的 我有一个JS函数类: function User() { //initiating object code goes here } //Functions User.prototype.getEmpId = function() { return this.empId; } var myUser = new User({'empid':'1'}); console.log(myUser.getE

我正在努力学习JavaScript中的面向对象编程。因此,我所做的可能是错误的

我有一个JS函数类:

function User() {
    //initiating object code goes here
}

//Functions
User.prototype.getEmpId = function() {
    return this.empId;
}

var myUser = new User({'empid':'1'});

console.log(myUser.getEmpId()); //Returns 1

var mynewuser = new User({'empid':'2'});

console.log(myUser.getEmpId()); //Returns 2

console.log(mynewuser.getEmpId()); //Returns 2
我不明白这里出了什么问题。如果这两个是两个不同的对象,那么为什么会导致问题。我已经添加了完整的代码

提前谢谢

User.prototype.getEmpId() {
  return this.empId;
}
应该是

User.prototype.getEmpId = function () {
  return this.empId;
}
应该是

User.prototype.getEmpId = function () {
  return this.empId;
}

每次实例化用户对象时,User.prototype.getEmpId都会被覆盖。prototype属性在所有实例之间共享

那怎么办呢:

User.prototype.getEmpId = function() {
  return this.empId;
}

每次实例化用户对象时,User.prototype.getEmpId都会被覆盖。prototype属性在所有实例之间共享

那怎么办呢:

User.prototype.getEmpId = function() {
  return this.empId;
}
检查这把小提琴:

现在回到您在示例共享中面临的问题。我发现每次实例化用户对象时,您都在User.prototype对象本身上设置属性/值。相反,您应该在每个用户实例上设置。以下是更新的JSFIDLE,它可以工作:

已完成的主要更改:

首先,

formUserObjectFromObject.call(this);//calling with reference of this
以及

希望有助于理解

检查这把小提琴:

现在回到您在示例共享中面临的问题。我发现每次实例化用户对象时,您都在User.prototype对象本身上设置属性/值。相反,您应该在每个用户实例上设置。以下是更新的JSFIDLE,它可以工作:

已完成的主要更改:

首先,

formUserObjectFromObject.call(this);//calling with reference of this
以及


希望有助于理解

你想检查一下这个吗。。不工作。。检查我的代码。它并不像beAah看起来的那么大..我知道你在哪里出错..实际上你根本没有在用户本身的实例上设置属性..你想检查一下吗。。不工作。。检查我的代码。它并不像beAah看起来的那么大..我知道你犯了什么错误..实际上你根本没有在用户本身的实例上设置属性..是的,但是empId属性属于实例本身,而不是它的原型。getEmpId只是一个返回该值的函数。那么在这种情况下该怎么办。?是的,但是empId属性属于实例本身,而不是它的原型。getEmpId只是一个返回该值的函数。那么在这种情况下该怎么办呢。?