• 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();

    结果:

  • 相关阅读:
    phpcms——列出父目录下的所有子目录问题
    chm格式文档不能阅读问题
    php学习 5 无限级分类
    phpcms——rss问题
    phpcms——评论页面修改
    phpcm后台评论管理修改评论列出条数
    php学习4 函数定义
    网站测试工具
    ASP.NET前台代码绑定后台变量方法总结收藏帖
    在asp.net网站下使用fckeditor 和fcfinder (包括修改fcfinder 来使上传文件按时间来命名和按用户分割文件)
  • 原文地址:https://www.cnblogs.com/yanyuanyuan/p/5749584.html
Copyright © 2020-2023  润新知