js面向对象

1.面向对象的概念:

     1)一切事物皆对象。

     2)对象具有封装和继承特性。

     3)信息隐藏。

~~~~~~~~~~~~~~~~~~~~~··

例:

    Literal.js

var person={

    name:"yanxiaoyuan",

    age:30,

    eat:function(){

            alert("能吃。");

    }

}

alert(person.name);

 

index.html

<script src="Listeral.js"></script>

结果:界面弹出提示框“yanxiaoyuan”.

~~~~~~~~~~~~~~~~~~~~~~

函数构造器构造对象:

function Person(){

}

person.prototype={

     name:"yanxioayuan",

     age:12,

    eat:function(){

         alert("我在吃。");

   }

var p=new Person();

 p.name//可以用这些参数调用

~~~~~~~~~~~~~~~~~~~~~~~

深入JAVASCRIPT的面向对象

例:function People(){

}

People.prototype.say=function(){

    alert("hello");

}

function Student(){

}

Student.prototype=new People();

var superSsay=Student.prototype.say;

Student.prototype.say=function(){

      superSsay.call(this);

      alert("stude-hello");

}

var s=new Student();

s.say();

结果:弹出提示框“hello”确定后,弹出"stude-hello"。

~~~~~~~~~~~~~~~~~

例:function People(name){

   this._name=name;

}

People.prototype.say=function(){

    alert("hello"+this._name);

}

function Student(name){

      this._name=name;

}

Student.prototype=new People();

var superSsay=Student.prototype.say;

Student.prototype.say=function(){

      superSsay.call(this);

      alert("stude-hello"+this._name);

}

var s=new Student("yanxioayuan");

s.say();

结果:

 

 

 ~~~~~~~~~~~~~~~~~~~~

例:隐藏例子

(function(){

var n="yanxioayuan";

function People(name){

   this._name=name;

}

People.prototype.say=function(){

    alert("hello"+this._name);

}

window.People=People;

}());

(function(){

function Student(name){

      this._name=name;

}

Student.prototype=new People();

var superSsay=Student.prototype.say;

Student.prototype.say=function(){

      superSsay.call(this);

      alert("stude-hello"+this._name+n);

}

window.Student=Student;

}());

 

var s=new Student("yanxioayuan");

s.say();

 结果:

注意:n为隐藏对象。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~····

例:(function(){

var name="yanxiaoyuan";

function Person(name){

    var _this={}

    _this._name=name;

   _this.sayHello=function(){

       alert("pHello"+_this._name+name);

  }

   return _this;

}

window.Person=Person;

}());

function Teacher(name){

    var _this=Person(name);

    var superSay=_this.sayHello;

   _this.sayHello=function(){

       superSay.call(_this);

       alert("THello"+_this._name);

  }

   return _this;

}

var t =Teacher("yanxiaoyuan");

t.sayHello();

结果:

 

 

转载于:https://www.cnblogs.com/yanyuanyuan/p/5749584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值