javascript 两种对象例子

javascript 有两种对象

1.内置对象

2.自定义对象

第一种方法new对象:

  var  student = new Object();

   student.name="张三";

   student.age=18;

   student.sex='男';

   student.show=function(){alert(this.name)};



   student.show();  //调用

   alert(student.age);

第二种方法字面量:

 var stu = {

  name:"李四",

  age:18,

  email:"3333@qq.com",

  show:function(){
    alert(this.age);

  }

 };



 stu.show();

第三种工厂模式:

function crateStudent(name, sex, age, hobby) {var stu = new Object();

​      stu.name = name;

​      stu.sex = sex;

​      stu.age = age;

​      stu.hobby = hobby;

​      stu.show = function () {alert(this.name);}return stu;}var stu1 = crateStudent("张三", "男", 18, "吃荷包蛋");

​    stu1.show();var stu2 = crateStudent("李四", "女", 22, "ctr");

​    stu2.show();

第四种构造函数

//创建构造函数  注意构造函数的名字一定要大写开头

function Student(name,sex,age){

  this.name=name;

  this.sex=sex;

  this.age=age;

  this.show=function(){alert(this.name);

  }

}

//调用构造函数

var stu1 = new Student("李琳","中间",88);

stu1.show(); //调用方法

第五种改写构造函数

其实就是把上面的方法抽出类公共的

function Student(name,sex,age){

  this.name=name;

  this.sex=sex;

  this.age=age;

  this.show=show;



}

function show(){

  alert(this.name);

}

var stu1 = new Student("李琳","中间",88);

stu1.show();

第六种写法原型对象

主要解决上面 私有方法抽出去变成了公开的方法的问题 使用原型对象创建的属性和方法 会共享

   //原型对象function Student() { }  //第一步创建构造函数Student.prototype.name = "李琳";  //通过prototype 来点出属性和方法Student.prototype.age = 27;Student.prototype.arr = [1, 2];Student.prototype.show = function () {alert(this.name);}var weitian = new Student();

​    weitian.arr.push(3); // 私人添加的3

​    weitian.name = "张三"; //私人的改成alert(weitian.arr);var haobing = new Student();alert(weitian.arr);  //1,2,3  说明引用类型的空间被共享 不会被私人占有其他的会被重写alert(weitian.name);  //张三

第六种写法 原型链 在原型对象的基础上多了一个继承

//继承

    function Person() { this.foot = 2; }Person.prototype.getFoot = function () {return this.foot;};



​    function Women() { this.head = 1 }Women.prototype = new Person();  // 继承Women.prototype.getHead = function () {return this.head;};var w =  new Women();alert(w.getFoot());alert(w instanceof Women);alert(w instanceof Person);alert(w instanceof Object);

重写

    function Person() { this.foot = 2; }Person.prototype.getFoot = function () {return this.foot;};function Women() { this.head = 1 }Women.prototype = new Person();  // 继承Women.prototype.getHead = function () {return this.head;};Women.prototype.getFoot= function (){  //重写父类的方法return false;}var w =  new Women();alert(w.getFoot());alert(w instanceof Women);alert(w instanceof Person);alert(w instanceof Object);

组合继承

    function Person(name) {this.name = name;}Person.prototype.show=function(){alert(this.name);}function Women() {Person.call(this,"zhangsan"); // 只能继承实例属性  能解决 引用类型共享变量问题 }Women.prototype=new Person();  //解决 方法继承的问题var a = new Women();

​    a.show();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值