JavaScript 对象属性


JavaScript 中的属性是不带对象的副值。下面是 JavaScript 中实现对象属性的代码 −

示例

 在线演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample {
      font-size: 18px;
      font-weight: 500;
      color: red;
   }
</style>
</head>
<body>
<h1>JavaScript Object Properties</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to display name and age property from testObj
object
</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   document.querySelector(".Btn").addEventListener("click", () => {
      let testObj = { name: "Rohan",age: 23,};
      sampleEle.innerHTML += "testObj.name = " + testObj.name + "<br>";
      sampleEle.innerHTML += "testObj.age = " + testObj.age + "<br>";
   });
</script>
</body>
</html>

输出

点击“单击此处”按钮 −

更新于:2020-5-11

153 次浏览

开启您的职业生涯

完成课程并获得认证

现在开始
广告