zoukankan      html  css  js  c++  java
  • javascript中五种方式实现方法的定义

    首先我们假设我们需要一个say方法用来说一句话,一个walk方法用来表示走了一步,那么下面让我们用五种方式实现这两个方法的定义。

    1.function say()

    {

    alert("hello world");

    }

    function walk()

    {

    alert("walk")

    }

    2.

    var Person=function(){};

    Person.propotyoe,say=function()

    {

    alert("hello world");

    }

    Person.prototype.walk=function()

    {

    alert("walk");

    }

    3.var Person={};

    Person.prototype={

    say:function(){

    alert("hello world")

    }

    walk:function()

    {

    alert("walk");

    }

    }

    4.

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

    {

     this.protype[name]=fn;

    }

    var Person=function(){};

    Person.method("say"function(){

    alert("hello world")

    })

    Person.method("walk"function(){

    alert("walk")

    })

    5.

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

    {

     this.protype[name]=fn;

    return this;

    }

    var Person=function(){};

    Person.method("say"function(){

    alert("hello")

    }).

    method("walk"function(){

    alert("walk")

    })

  • 相关阅读:
    软件工程第二次作业
    软件工程第一次作业
    配色一览
    软件工程(2018)第二次个人作业
    Android ImageView设置图片
    Android 打开网络连接
    Android 传感器简记
    适配Android O的通知
    Android日记打包
    Android轻量数据库
  • 原文地址:https://www.cnblogs.com/heshan664754022/p/2250069.html
Copyright © 2011-2022 走看看