Home  >  Article  >  Web Front-end  >  JavaScript implementation of accessing object properties (getting/setting property values)

JavaScript implementation of accessing object properties (getting/setting property values)

不言
不言Original
2018-11-30 16:50:454300browse

通过在JavaScript implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values)中定义对象,可以像类一样实现封装,在本文中将给大家介绍如何编写代码来访问对象属性,下面我们来看具体的内容。

JavaScript implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values)

我们先来看一下格式

(对象的实例名称).(属性名称)

我们来看具体的示例

代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function product() {
      this.name = "LemonCandy";
      this.price = 150;
      this.getInfo = function(){
      return this.name + " = \\" + this.price;
          }
        }
        window.onload = function onLoad() {
            target = document.getElementById("output");
            prod = new product();
            target.innerHTML = "name:" + prod.name + "<br/>";
            target.innerHTML += "price:" + prod.price + "<br/>";
        }
  </script>
</head>
<body>
  <div id="output"></div>
</body>
</html>

说明

function product() {
    this.name = "LemonCandy";    
    this.price = 150;    
    this.getInfo = function(){      
    return this.name + " = \\" + this.price;
    }
  }

上面的代码是对象的定义,在这个例子中,我们实现了name,price属性和getInfo()方法。

window.onload = function onLoad() {
      target = document.getElementById("output");
      prod = new product();
      prod.name = "LemonCandy2";
      target.innerHTML = "name:" + prod.name + "<br/>";
      target.innerHTML += "price:" + prod.price + "<br/>";
  }

加载页面时,执行上面的代码。

prod = new product();

创建对象的实例。

prod.name = "LemonCandy2";

(对象的实例名称).(属性名称)用于访问对象的属性。要为属性设置值,需要通过替换设置属性值

 target.innerHTML = "name:" + prod.name + "<br/>";
      target.innerHTML += "price:" + prod.price + "<br/>";

(对象的实例名称).(属性名称)用于访问对象的属性。属性的值显示在页面上。

运行结果

当Web浏览器显示上述HTML文件时,将显示如下所示的效果。可以确认设定的属性值和从属性中获取值。

JavaScript implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values)

接下来我们再来看一个例子

代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
var obj = {
      name: "LemonCandy",
      price: 150,
      getInfo: function () {
        return this.name + " = \\" + this.price;
      }
    }
    window.onload = function onLoad() {
      target = document.getElementById("output");
      obj.name = "LemonCandy2";
      obj.price = 220;
      target.innerHTML = "name:" + obj.name + "<br/>";
      target.innerHTML += "price:" + obj.price + "<br/>";
    }
  </script>
</head>
<body>
<div id="output"></div>
</body>
</html>

说明

var obj = {
    name: "LemonCandy",    
    price: 150,    
    getInfo: function () {
          return this.name + " = \\" + this.price;
    }
  }

上面的代码是对象的定义。在这个例子中,我们实现了name,price属性和getInfo()方法。它是以简单的格式编写的。

window.onload = function onLoad() {
    target = document.getElementById("output");
    obj.name = "LemonCandy2";
    obj.price = 220;
    target.innerHTML = "name:" + obj.name + "<br/>";
    target.innerHTML += "price:" + obj.price + "<br/>";
  }

加载页面时,执行上面的代码。代码与例1中的代码相同,处理内容相同。因为使用的是简单的类型的对象定义的格式,不存在由new命令创建一个实例部分这与例1中的代码不同。

运行结果

当Web浏览器显示上述HTML文件时,将显示如下所示的效果,确认可以将值设置为属性并从属性中获取值。

JavaScript implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values) implementation of accessing object properties (getting/setting property values)

The above is the detailed content of JavaScript implementation of accessing object properties (getting/setting property values). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn