动态访问函数对象属性 [英] Dynamically access function object property

查看:116
本文介绍了动态访问函数对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查多个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); //作品

如果 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

您也不需要 new 在你的函数前面,因为你没有使用系统创建的对象 - 你只是创建自己的对象并返回它。它仍然可以与 new 一起工作,但是由于您没有使用系统将使用 new 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};
}

或者使用ES6语法:

 函数make_test(name,job,ID){
return {name,job,ID};
}


I need to check if the value of multiple textareas is equal to the property 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

It should only be a match if the 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.

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 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).

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.

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"

You also don't need 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.

If you want it to be an actual constructor function where you use 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);

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:

function make_test(name, job, ID) {
    return {name: name, job: job, ID: ID};
}

Or using ES6 syntax:

function make_test(name, job, ID) {
    return {name, job, ID};
}

这篇关于动态访问函数对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆