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