如何在java脚本中将输入变量分配给对象的成员

时间:2015-10-14 17:27:08

标签: javascript

我创建了一个带有两个变量字符串,一个数字和另一个浮点数的类函数。对于新对象,当我尝试将用户输入分配给此新对象的每个变量时,系统无法定义它。 alert(s[0])和其他人工作正常并分开项目,但之后无法将它们分配给功能b成员。有人可以帮忙吗?

var Book = function() {var name, author , year, price };
var b = new Book;
b.name = "abc";
b.author = "aaa";
b.year = 0;
b.price = 0.0;
var s;
b =prompt("pls enter name, author, year, price ");
s = b.split(";");
alert(s[0]);
alert(s[1]);
alert(s[2]);
alert(s[3]);
if (s.length = 4){
    b.name = s[0];
    b.author = s[1];
    b.year = s[2];
    b.price = s[3];
}

1 个答案:

答案 0 :(得分:0)

您应该在split变量而不是myBook上执行b



var Book= function() {var name, author , year, price };
var b = new Book;
b.name ="abc";
b.author= "aaa";
b.year=0;
b.price=0.0;
var  s;
myBook=prompt("pls enter name, author, year, price ");
s = myBook.split(";");
alert(s[0]);
alert(s[1]);
alert(s[2]);
alert(s[3]);
    if (s.length == 4){
       b.name = s[0];
       b.author = s[1];
       b.year = s[2] ;
       b.price = s[3] ;
    }
console.log(b);