0

我尝试了几件事,包括使用构造函数和下面的代码,都没有奏效。我以为这很简单,我该怎么做?

<!DOCTYPE html>
<html>
<body>

<p>Creating a JavaScript Object.</p>

<p id="demo"></p>

<script>
var name ={ // subobject
    firstName : "John",
    lastName  : "Doe",
};
var person = {
name: {} // want to append here
};
person.name = name;
document.getElementById("demo").innerHTML =
person.name.firstName + " " + person.name.lastName;
</script>

</body>
</html>
4

1 回答 1

0

只需使用变量或子对象本身:

var person = {
    name: name
};

或者,如果您使用子对象,

var person = { 
    name: { // subobject
        firstName : "John",
        lastName  : "Doe",
    }
};
于 2015-01-03T03:01:21.560 回答