Array对象的三种属性实例

本文详细介绍了JavaScript中的length属性、prototype属性及constructor属性。通过具体实例展示了如何使用这些属性,并解释了它们的功能和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

length 属性

定义和用法

length 属性可设置或返回数组中元素的数目。这个很常见了

语法

arrayObject.length

prototype 属性

定义和用法

prototype 属性使您有能力向对象添加属性和方法。

语法

object.prototype.name=value

实例

在本例中,我们将展示如何使用 prototype 属性来向对象添加属性:

  

 1  <script type="text/javascript">
 2     //构造函数
 3   function employee(name,job,born)
 4   {
 5   this.name=name;
 6   this.job=job;
 7   this.born=born;
 8   }
 9   //new一个新的实例,bill,他继承了了employee的所有属性
10  var bill=new employee("Bill Gates","Engineer",1985);
11  //向employee原型上添加salary属性
12  employee.prototype.salary=null;
13   //这个属性可以被bill继承到
14  bill.salary=20000;
15  
16  document.write(bill.salary);
17  
18  </script>

 

输出:

20000

constructor属性
constructor 属性返回对创建此对象的数组函数的引用。
<script type="text/javascript">
//构造函数
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.constructor);

</script>

输出:

1 function employee(name, jobtitle, born)
2 {this.name = name; this.jobtitle = job; this.born = born;}

 从结果看来,就是返回创建它的构造函数

转载于:https://www.cnblogs.com/nostic/p/5406377.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值