-- 原型:prototype的使用

在 中,prototype 是函数的一个属性,同时也是由构造函数创建的对象的一个属性。 函数的原型为对象。 它主要在函数用作构造函数时使用。

可以使用 prototype 属性向对象添加属性和方法,甚至于已创建的对象也是如此

1. 举例:

<!doctype html>
<html>
 <head>
  <  charset=\"UTF-8\">
  < >Document</ >
  <  type=\"text/ \">
        function Person(name,age,email)
        {
            this.Name=name;
            this.Age=age;
            this.Email=email;
        }

        var p1= new Person(\'小胡子\',20,\'xiaohuzi@test.com\');
        Person.prototype.Address=\'China\';
        alert(p1.Name+\'  \'+p1.Age+\'  \'+p1.Email+\'  \'+p1.Address);
  </ >
 </head>
 <body>
  

2. 运行结果:

\"\"

 

收藏 打印