-4

我想打印鲍勃史密斯和他的年龄,我该怎么做?该示例是 codecademy 上面向对象系列的一部分,任何帮助将不胜感激。

function Person(name, age) {
    this.name = name;
    this.age = age;
};
// Let's make bob again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 35);
// make your own class here
console.log(this.name);
4

2 回答 2

2

If you want to print Bob's name, then print bob's name:

console.log(bob.name);
于 2013-07-13T10:07:28.790 回答
0
function Person(name, age) {
    this.name = name || '';
    this.age = age || -1;
};

Person.prototype.toString = function(){
    return this.name + ' ' + this.age;
};

var bob = new Person("Bob Smith", 30);

console.log(bob.toString());
alert(bob);
于 2013-07-13T10:42:38.393 回答