<script type="text/javascript">
function Cat()
{
this.name1 = "大毛1";
this.getname1 = function() { return this.name1 + "xxx"; };
}
Cat.prototype.name2 = "大毛2";
Cat.prototype.getname2 = function() { return this.name2 + "xxx"; }
var xxx = new Cat();
alert(xxx.name1);
alert(xxx.name2);
alert(xxx.getname1());
alert(xxx.getname2());
</script>
<script type="text/javascript">
var Cat =
{
name1: "大毛1", // 属性
name2: "大毛2", // 属性
getname1: function(){ return this.name1; }, // 方法
getname2: function(){ return this.name2; } // 方法
};
Cat.name3 = "123456";
Cat.getname3 = function() { return this.name3; };
alert(Cat.getname1()); // 静态
var C1 = Object.create(Cat);
alert(C1.getname2()); // 大毛
</script>
<script type="text/javascript">
var Cat =
{
createNew: function()
{
var classx = {}; // 定义类
classx.name = "大毛";
classx.makeSound = function() { alert("喵喵喵"); };
return classx;
}
};
var cat1 = Cat.createNew();
cat1.makeSound();
</script>