现在的位置: 首页 > 综合 > 正文

java web学习笔记(javascript继承)

2013年11月21日 ⁄ 综合 ⁄ 共 767字 ⁄ 字号 评论关闭

 1、对象冒充,不推荐使用

  function Parent(username) {

        this.nameuser = username;

        this.sayHello() {

                 alert(username);

        }

  }

  function Child(username, password) {

        this.method = Parent;

        this.method(username);

        delete this.method;

  }

  2、使用call方法,Function对象中的方法

  function say(str) {

        alert(this.name + '=' + str);

  }

  var ob = new Object();

  obj.name = 'JPass';

  say.call(obj, 'long'); // 此方法其实是将obj对象赋值给了this

 3、使用apply方法实现对象的继承

 function Parent(username) {

        this.nameuser = username;

        this.sayHello() {

                 alert(username);

        }

  }

  function Child(username, password) {

       Parent.apply(this, new Array(username));

  }

4、使用原型链的方式是想对象间的继承

function Parent() {

  }

  Parent.prototype.name = 'JPass';

  Parent.prototype.sayName = function() {

        alert(this.name);

  }

  function Child() {

  }

  Child.prototype = new Parent();

抱歉!评论已关闭.