zoukankan      html  css  js  c++  java
  • javascript面向对象起步

    注:console.log("")只能在Firefox,或 chrome 中firebug调试其他地方请换成 alert("")

    /* 起步之一直接写成函数 此时为面向过程 */

    function startAnimalation() {

    console.log(
    "start");

    }

    function stopAnimalation() {

    console.log(
    "stop");

    }

    /* 起步之二*/

    var Animal
    = function() {

    console.log(
    "constructor");

    };

    Animal.prototype.start
    = function() {

    console.log(
    "start");

    };

    Animal.prototype.stop
    = function() {

    console.log(
    "");

    };

    /* 用法 */

    var myAnimal
    = new Animal();

    myAnimal.start();

    myAnimal.stop();

    /* 起步之三 方法直接打包到prototype */

    var Animal
    = function() {

    console.log(
    "constructor");

    };

    Animal.prototype
    = {

    start: function() {

    console.log(
    "start");

    },

    stop: function() {

    console.log(
    "stop");

    }

    };

    /* 起步之四 拓展Function方法 可以动态添加 方法或属性 到prototype之中*/

    Function.prototype.method
    = function(name, fn) {

    this.prototype[name] = fn;

    };

    var Animal
    = function() {

    console.log(
    "");

    };

    Animal.method(
    'start', function() {

    console.log(
    "");

    });

    Animal.method(
    'stop', function() {

    console.log(
    "");

    });

    /* 起步之五 支持“.” 拓展 */

    Function.prototype.method
    = function(name, fn) {

    this.prototype[name] = fn;

    return this;

    };

    var Animal
    = function() {

    console.log(
    "");

    };

    Animal. method(
    'start', function() {console.log("");}). method('stop', function() {console.log(""); });

      

  • 相关阅读:
    老了老了呜呜呜呜
    我们的焦点在哪里
    visual studio 2010
    饥饿游戏2 影评
    两个极端
    关于怀旧
    进程 线程 碎角料
    拥塞控制
    [zz] 几种类间关系:继承、实现、依赖、关联、聚合、组合及UML实现图
    Markdown 的使用
  • 原文地址:https://www.cnblogs.com/senion/p/2169193.html
Copyright © 2011-2022 走看看