动态访问函数对象属性 [英] Dynamically access function object property
问题描述
我需要检查多个textareas的值是否等于此对象的属性 name
:
函数make_test(name,job,ID){
test = {};
test.name = name;
test.job = job;
test.ID = ID;
return test;
}
new make_test(Paul,manager,1); //不起作用
new make_test(John,employee,2); //不起作用
new make_test(Jan,employee,2); //作品
如果值$如果textarea的索引等于人的
ID
,则c $ c>等于名称和。例如,如果我在 textarea1
中输入Paul,它应该输出paul的作业:但它不应该输出到 textarea2
应该只输出 ID
= 2的人的工作。
问题:我的代码只适用于最后一个人声明(在这个例子中是Jan)。这就像其他人甚至不存在于对象中,除了最后一个。我该如何解决这个问题?
我相信答案很明显,但我无法弄清楚我做错了什么。
这里演示: https://jsfiddle.net/ Lau1989 / hxcpstty /
感谢您的帮助
你需要声明 test
是一个局部变量,所以你的函数的所有调用都没有引用完全相同的全局变量。当你不声明你的变量时,它变成了一个隐含的全局变量,它可能导致各种各样的问题。如果您在 严格模式
,解释器会将这标记为您的错误(这通常很有用)。
您还需要指定将结果从你的函数返回给一个变量,这样你就可以引用新创建的对象。
,ID){var test = {};
// ^^^
test.name = name;
test.job = job;
test.ID = ID;
return test;
}
var o1 = make_test(Paul,manager,1);
var o2 = make_test(John,employee,2);
var o3 = make_test(Jan,employee,2);
console.log(o1.name); //Paul
console.log(o3.job); //employee
您也不需要 请注意,我在这里使用了大写的构造函数名称,因为使用初始帽名称 您也可以完全删除声明: 或者使用ES6语法: I need to check if the value of multiple textareas is equal to the property It should only be a match if the Problem : my code only works for the last person declared (Jan in this example). It's like the other persons don't even exist in the object, except for the last one. How can I fix this ? I'm sure the answer is pretty obvious but I can't figure out what I'm doing wrong. Demo here : https://jsfiddle.net/Lau1989/hxcpstty/ Thanks for your help You need to declare You also need to assign the return result from your function to a variable so you can reference the newly created object using that variable. You also don't need If you want it to be an actual constructor function where you use Note, I used a capitalized constructor name here since it is a common convention to use initial cap names for constructor functions and initial lowercase names for regular functions. You could also just remove the declaration entirely: Or using ES6 syntax:
这篇关于动态访问函数对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! new
在你的函数前面,因为你没有使用系统创建的对象 - 你只是创建自己的对象并返回它。它仍然可以与 new
一起工作,但是由于您没有使用系统将使用 new $ c $创建的对象如果你想让它成为一个实际的构造函数,你可以使用
new
,并且可以继承原型。 ,然后执行以下操作:
$ p $ 函数Make_test(name,job,ID){
this.name = name;
this.job = job;
this.ID = ID;
}
var o1 = new Make_test(Paul,manager,1);
var o2 = new Make_test(John,employee,2);
var o3 = new Make_test(Jan,employee,2);
函数make_test(name,job,ID){
return {name:name,job:job,ID:ID};
}
函数make_test(name,job,ID){
return {name,job,ID};
}
name
of this object :function make_test(name, job, ID) {
test = {};
test.name = name;
test.job = job;
test.ID = ID;
return test;
}
new make_test("Paul", "manager", 1); //doesn't work
new make_test("John", "employee", 2); //doesn't work
new make_test("Jan", "employee", 2); //works
value
is equal to the name and if the index of the textarea is equal to the person's ID
. For instance if I type "Paul" in textarea1
, it should output paul's job : but it should not output it in textarea2
which should only output the job of persons having an ID
= 2.test
to be a local variable so all invocations of your function are not referring to the exact same global variable. When you don't declare your variable it becomes an implicit global variable which can lead to all sorts of problems. If you run your code in strict mode
, the interpreter will flag this as an error for you too (which is generally helpful).function make_test(name, job, ID) {
var test = {};
// ^^^
test.name = name;
test.job = job;
test.ID = ID;
return test;
}
var o1 = make_test("Paul", "manager", 1);
var o2 = make_test("John", "employee", 2);
var o3 = make_test("Jan", "employee", 2);
console.log(o1.name); // "Paul"
console.log(o3.job); // "employee"
new
in front of your function since you aren't using a system created object - you are just creating your own object and returning it. It will still work with new
, but it is wasteful since you aren't using the object that the system will create with new
.new
and could inherit the prototype, then do this:function Make_test(name, job, ID) {
this.name = name;
this.job = job;
this.ID = ID;
}
var o1 = new Make_test("Paul", "manager", 1);
var o2 = new Make_test("John", "employee", 2);
var o3 = new Make_test("Jan", "employee", 2);
function make_test(name, job, ID) {
return {name: name, job: job, ID: ID};
}
function make_test(name, job, ID) {
return {name, job, ID};
}