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(""); });

      

  • 相关阅读:
    地址栏中提交中文参数乱码问题
    拼接html字符串时单引号问题
    细线表格的制作
    盒子模型
    盒子间距离的计算规则:
    正则表达式
    轻便+智能:史上最酷恒温器Nest 2.0!
    医疗的未来,是身体控制大权的争夺战
    而立之年话沧桑
    刘晓明大使在《电讯报》的英文原文
  • 原文地址:https://www.cnblogs.com/senion/p/2169193.html
Copyright © 2011-2022 走看看