{"id":644,"date":"2008-12-14T15:12:52","date_gmt":"2008-12-14T07:12:52","guid":{"rendered":"http:\/\/fdream.net\/blog\/article\/644.aspx"},"modified":"2008-12-14T15:12:11","modified_gmt":"2008-12-14T07:12:11","slug":"Mootools+1.2%e6%95%99%e7%a8%8b(21)%e2%80%94%e2%80%94%e7%b1%bb%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/644","title":{"rendered":"Mootools 1.2\u6559\u7a0b(21)\u2014\u2014\u7c7b\uff08\u4e8c\uff09"},"content":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.consideropen.com\/blog\/2008\/09\/30-days-of-mootools-12-tutorials-day-21-classes-part-ii\/\" target=\"_blank\" title=\"30 Days of Mootools 1.2 Tutorials - Day 21 - Classes part II\">30 Days of Mootools 1.2 Tutorials &#8211; Day 21 &#8211; Classes part II<\/a><\/p>\n<h2>\u7c7b\u2014\u2014\u7b2c\u4e8c\u90e8\u5206<\/h2>\n<p>\u8bf7\u5c0a\u91cd\u4e2a\u4eba\u52b3\u52a8\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttp:\/\/fdream.net\uff0c\u8bd1\u8005\uff1aFdream<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u51c6\u5907\u597d\u5f00\u59cb\u8fd9\u4e00\u8bb2\uff0c\u8bf7\u53c2\u8003\u8fd9\u4e00\u7cfb\u5217\u7684\u6559\u7a0b\uff0c\u8fd9\u91cc\u662f<a href=\"http:\/\/fdream.net\/blog\/article\/605.aspx\" target=\"_blank\" title=\"\u300aMooTools 1.2\u7cfb\u5217\u6559\u7a0b\u76ee\u5f55\u300b\">\u300aMooTools 1.2\u7cfb\u5217\u6559\u7a0b\u76ee\u5f55\u300b<\/a>\u3002<\/p>\n<p>Fdream\u6ce8\uff1a\u539f\u6587\u597d\u50cf\u5c11\u4e86\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u6211\u81ea\u4f5c\u4e3b\u5f20\u5730\u8865\u5145\u4e86\u4e0b\u9762\u8fd9\u4e00\u6bb5\u53caBaseClass\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u4eca\u5929\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u5b66\u4e60\u4e00\u4e0bMooTools\u4e2d\u7c7b\u7684\u5b9e\u73b0\u548c\u7ee7\u627f\uff08\u6269\u5c55\uff09\u3002\u901a\u8fc7\u5b9e\u73b0\u548c\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u4f7f\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u518d\u91cd\u65b0\u58f0\u660e\u548c\u5b9e\u73b0\u76f8\u540c\u7684\u65b9\u6cd5\u3002MooTools\u4e2d\u7684Class\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e00\u4e2a\u57fa\u7c7b\uff08base class\uff09\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar BaseClass = new Class({\n    \/\/ \u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5testFunction\n    \/\/ \u8fd9\u4e2a\u65b9\u6cd5\u5f39\u51fa\u4e00\u4e2a\u5bf9\u8bdd\u6846\n    testFunction : function(){\n        alert('This function is defined in BaseClass');\n    }\n});\n<\/pre>\n<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a\u57fa\u7c7b\uff08base class\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u7c7b\u6765\u5b9e\u73b0\u5b83\u4ece\u800c\u4f7f\u7528\u5b83\u7684\u5168\u90e8\u529f\u80fd\u3002\u6ce8\u610f\uff0c\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7684\u65b0\u7c7b\u6ca1\u6709\u505a\u4efb\u4f55\u4e8b\u60c5\uff0c\u800c\u53ea\u662f\u5b9e\u73b0\u4e86\u57fa\u7c7bBaseClass\u3002<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u521b\u5efa\u4e00\u4e2a\u540d\u53ebImplementingClass\u7684\u7c7b\nvar ImplementingClass = new Class({\n    \/\/ \u6211\u6240\u505a\u7684\u5168\u90e8\u4e8b\u60c5\u5c31\u662f\u5b9e\u73b0Baseclass\n    Implements : BaseClass\n});\n<\/pre>\n<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aImplementingClass\u7684\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528\u5728BaseClass\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar demo_one = function(){\n    \/\/ \u521b\u5efa\u4e00\u4e2aImplementingClass\u5b9e\u4f8b\n    var test_class = new ImplementingClass();\n    \/\/ \u8c03\u7528\u5728BaseClass\u4e2d\u5b9a\u4e49\u7684testFunction\n    test_class.testFunction();\n}\n<\/pre>\n<\/p>\n<p>\n<script type=\"text\/javascript\">\nvar BaseClass_1 = new Class({\n  testFunction : function(){\n    alert('This function is defined in BaseClass');\n  }\n});\nvar ImplementingClass_1 = new Class({\n  Implements : BaseClass_1\n}); \nvar demo_one = function(){\n  var test_class = new ImplementingClass_1();\n  test_class.testFunction();\n}\nvar BaseClass_2 = new Class({\n  initialize: function(input){\n        this.inputVariable = input;\n    },  \n  testFunction : function(){\n    alert('BaseClass.testFunction() : ' + this.inputVariable);\n  },\n  definedVariable : \"Defined in BaseClass\",\n}); \nvar ImplementingClass_2 = new Class({\n  Implements : BaseClass_2\n}); \nvar demo_two = function(){\n  var test_class = new ImplementingClass_2('this is the input value');\n  test_class.testFunction();\n  alert('test_class.testVariable : ' + test_class.definedVariable);\n}\nvar ImplementingClass_3 = new Class({\n  Implements : BaseClass_2,\n  definedVariable : \"Defined in ImplementingClass\",\n  testFunction : function(){\n    alert('This function is also defined in BaseClass');\n  },\n  anotherDefinedVariable : \"Also Defined in ImplementingClass\", \n  anotherTestFunction : function(){\n    alert('This function is defined in ImplementingClass');\n  }\n}); \nvar demo_three = function(){\n  var test_class = new ImplementingClass_3('this is the input value');  \n  test_class.testFunction();\n  alert('test_class.testVariable : ' + test_class.definedVariable);\n  test_class.anotherTestFunction();\n  alert('test_class.anotherDefinedVariable : ' + test_class.anotherDefinedVariable);\n}\nvar ExtendingClass_1 = new Class({\n  Extends : BaseClass_2,\n  definedVariable : \"Defined in ImplementingClass\",\n  testFunction : function(){\n    alert('This function is also defined in BaseClass');\n  }\n}); \nvar demo_four = function(){\n  var test_class = new ExtendingClass_1('this is the input value');\n  test_class.testFunction();\n  alert('test_class.definedVariable : ' + test_class.definedVariable);\n}\nvar ExtendingClass_2 = new Class({\n  Extends : BaseClass_2,\n  initialize: function(input){\n    this.parent(input);\n    this.otherVariable = \"Original Input Was : \" + input;\n  }\n}); \nvar demo_five = function(){\n  var test_class = new ExtendingClass_2('this is the input value');  \n  test_class.testFunction();\n  alert(\"test_class.otherVariable : \" + test_class.otherVariable);\n}\nvar Calculator = new Class({\n  initialize: function(first_number, second_number){\n    this.first  = first_number;\n    this.second = second_number;\n  },\n  add : function(){\n    result = this.first + this.second;\n    alert(result);\n  },\n  subtract : function(){\n    result = this.first - this.second;\n    alert(result);\n  }  \n}); \nvar demo_six = function(){\n  Calculator.implement({\n    multiply : function(){\n      result = this.first * this.second;\n      alert(result);\n    }\n  });  \n  var myCalculator = new Calculator(100, 50);\n  myCalculator.multiply();\n}\nvar demo_seven = function(){\n  Calculator.implement({\n    show_class : function(){\n      alert(print_r(this, true));\n    }\n  });\n  var myCalculator = new Calculator(100, 50);\n  myCalculator.show_class();\n}\nvar demo_eight = function(){\n  Element.implement({\n    showStructure : function(){\n      var structure = '<\/p>\n<pre>' + print_r(this, true) + '<\/pre>\n<p>'; \n      newWindow = window.open('','Element Debug','height=600,width=600,scrollbars=yes');\n      newWindow.document.write(structure);      \n    }\n  });\n  $('demo_eight_button').showStructure();\n}\nwindow.addEvent('domready', function(){\n  $('demo_one_button').addEvent('click', demo_one);\n  $('demo_two_button').addEvent('click', demo_two);\n  $('demo_three_button').addEvent('click', demo_three);\n  $('demo_four_button').addEvent('click', demo_four);\n  $('demo_five_button').addEvent('click', demo_five);\n  $('demo_six_button').addEvent('click', demo_six);\n  $('demo_seven_button').addEvent('click', demo_seven);\n  $('demo_eight_button').addEvent('click', demo_eight);\n}); \nfunction print_r( array, return_val ) {    \n    var output = \"\", pad_char = \" \", pad_val = 4; \n    var formatArray = function (obj, cur_depth, pad_val, pad_char) {\n        if (cur_depth > 0) {\n            cur_depth++;\n        } \n        var base_pad = repeat_char(pad_val*cur_depth, pad_char);\n        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);\n        var str = \"\"; \n        if (obj instanceof Array || obj instanceof Object) {\n            str += \"Array\\n\" + base_pad + \"(\\n\";\n            for (var key in obj) {\n                if (obj[key] instanceof Array) {\n                    str += thick_pad + \"[\"+key+\"] => \"+formatArray(obj[key], cur_depth+1, pad_val, pad_char);\n                } else {\n                    str += thick_pad + \"[\"+key+\"] => \" + obj[key] + \"\\n\";\n                }\n            }\n            str += base_pad + \")\\n\";\n        } else if(obj == null || obj == undefined) {\n            str = '';\n        } else {\n            str = obj.toString();\n        } \n        return str;\n    }; \n    var repeat_char = function (len, pad_char) {\n        var str = \"\";\n        for(var i=0; i < len; i++) { \n            str += pad_char; \n        };\n        return str;\n    };\n    output = formatArray(array, 0, pad_val, pad_char); \n    if (return_val !== true) {\n        document.write(\"\n\n<pre>\" + output + \"<\/pre>\n<p>\");\n        return true;\n    } else {\n        return output;\n    }\n}\n<\/script><br \/>\n<button id=\"demo_one_button\">demo_one()<\/button>\n<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u5bf9\u57fa\u7c7b\u4e2d\u7684\u53d8\u91cf\u548c\u521d\u59cb\u5316\u51fd\u6570\uff08\u6784\u9020\u51fd\u6570\uff09\u505a\u540c\u6837\u7684\u4e8b\u60c5\u3002\u5982\u679c\u4f60\u5728\u5b9e\u65bd\u7c7b\uff08\u5b9e\u73b0\u57fa\u7c7b\u7684\u7c7b\uff09\u4e2d\u58f0\u660e\u4e86\u7684\u8bdd\uff0c\u6bcf\u4e2a\u4f60\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u4e1c\u897f\u5c31\u5c06\u88ab\u8f6c\u79fb\u5230\u5b9e\u65bd\u7c7b\u4e2d\u3002<\/p>\n<p><b>\u6ce8\u610f\uff1a<\/b>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u4e0b\u9762\u6240\u6709\u7684\u793a\u4f8b\u4e2d\u5c06\u5168\u90e8\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2a\u7248\u672c\u7684BaseClass\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar BaseClass = new Class({\n    \/\/ \u628a\u53c2\u6570\u8d4b\u503c\u7ed9\u8fd9\u4e2a\u7c7b\u4e2d\u7684inputVariable\u53d8\u91cf\n    initialize: function(input){\n        this.inputVariable = input;\n    },\n    \/\/ \u663e\u793a\u53d8\u91cfinputVariable\u7684\u503c\n    testFunction : function(){\n        alert('BaseClass.testFunction() : ' + this.inputVariable);\n    },\n    \/\/ \u4e3a\u8fd9\u4e2a\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u53d8\u91cf\n    definedVariable : &quot;Defined in BaseClass&quot;,\n});\n \nvar ImplementingClass = new Class({\n    \/\/ \u91cd\u590d\u4e00\u904d\uff1a\n    \/\/ \u8fd9\u91cc\u6211\u4eec\u505a\u7684\u5168\u90e8\u4e8b\u60c5\u5c31\u53ea\u662f\u5b9e\u73b0BaseClass\n    Implements : BaseClass\n});\n<\/pre>\n<\/p>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u8868\u660e\uff1a\u521d\u59cb\u5316\u7a0b\u5e8f\u3001\u51fd\u6570\u8c03\u7528\u548c\u53d8\u91cf\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\u5230\uff0c\u5c31\u50cf\u5b83\u4eec\u662f\u5c5e\u4e8e\u8fd9\u4e2a\u5b9e\u65bd\u7c7b\u7684\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar demo_two = function(){\n    \/\/ \u521b\u5efa\u4e00\u4e2aImplementingClass\u5b9e\u4f8b\n    var test_class = new ImplementingClass('this is the input value');\n \n    \/\/ \u8c03\u7528testFunction()\uff08\u5728BaseClass\u4e2d\u5b9a\u4e49\u7684\uff09\n    test_class.testFunction();\n \n    \/\/ \u663e\u793a\u53d8\u91cfdefinedVariable\u7684\u503c\n    alert('test_class.testVariable : ' + test_class.definedVariable);\n}\n<\/pre>\n<\/p>\n<p>\n<button id=\"demo_two_button\">demo_two()<\/button>\n<\/p>\n<p>\u4e00\u65e6\u4f60\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7c7b\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u529f\u80fd\u5230\u4f60\u7684\u5b9e\u65bd\u7c7b\u5b9a\u4e49\u4e2d\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar ImplementingClass = new Class({\n    Implements : BaseClass,\n    \/\/ \u4e0b\u9762\u7684\u8fd9\u4e9b\u529f\u80fd\u90fd\u5728BaseClass\u4e2d\u5b9a\u4e49\u4e86\n    definedVariable : &quot;Defined in ImplementingClass&quot;,\n    testFunction : function(){\n        alert('This function is also defined in BaseClass');\n    },\n \n    \/\/ \u4e0b\u9762\u7684\u8fd9\u4e9b\u90fd\u6ca1\u6709\u5728BaseClass\u4e2d\u5b9a\u4e49\n    anotherDefinedVariable : &quot;Also Defined in ImplementingClass&quot;,\n    anotherTestFunction : function(){\n        alert('This function is defined in ImplementingClass');\n    }\n});\n<\/pre>\n<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u5728\u5b9e\u65bd\u7c7b\u4e2d\u91cd\u65b0\u5b9a\u4e49\u4e86testFunction\u548cdefinedVariable\uff0c\u5c31\u50cf\u6211\u4eec\u6dfb\u52a0\u65b0\u7684\u51fd\u6570\u548c\u53d8\u91cf\u4e00\u6837\u3002\u7279\u522b\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<b>\u5982\u679c\u4f60\u60f3\u5728\u5b9e\u65bd\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5df2\u7ecf\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u7684\u51fd\u6570\u6216\u8005\u53d8\u91cf\uff0c\u57fa\u7c7b\u4e2d\u7684\u5b9a\u4e49\u5c06\u4f1a\u53d6\u4ee3\u5b9e\u65bd\u7c7b\u4e2d\u7684\u5b9a\u4e49\u3002<\/b>\u5982\u679c\u4e0d\u660e\u767d\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u5c31\u77e5\u9053\u4e86\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar demo_three = function(){\n    \/\/ \u521b\u5efa\u4e00\u4e2aImplementingClass\u5b9e\u4f8b\n    var test_class = new ImplementingClass('this is the input value');\n \n    \/\/ (\u6267\u884cBaseClass\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\uff09\n    test_class.testFunction();\n \n    \/\/ \u663e\u793a\u53d8\u91cfdefinedVariable\u7684\u503c\uff08BaseClass\u4e2d\u5b9a\u4e49\u7684\u503c\uff09\n    alert('test_class.testVariable : ' + test_class.definedVariable);\n \n    \/\/ \uff08ImplementingClass\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\uff09\n    test_class.anotherTestFunction();\n \n    \/\/ \u663e\u793a\u53d8\u91cfanotherDefinedVariable\u7684\u503c\uff08ImplementingClass\u4e2d\u5b9a\u4e49\u7684\u503c\uff09\n    alert('test_class.anotherDefinedVariable : ' + test_class.anotherDefinedVariable);\n}\n<\/pre>\n<\/p>\n<p>\n<button id=\"demo_three_button\">demo_three()<\/button>\n<\/p>\n<h3>Extends\uff08\u6269\u5c55\uff09<\/h3>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u8986\u76d6\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Extends\u3002\u7b80\u5355\u5730\u628a\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u201cImplements\u201d\u66ff\u6362\u6210\u201cExtends\u201d\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar ExtendingClass = new Class({\n    \/\/ \u6ce8\u610f\u8fd9\u91cc\u7528Extends\u66ff\u4ee3\u4e86Implements\n    Extends : BaseClass,\n    \/\/ \u4e0b\u9762\u7684\u8fd9\u4e9b\u90fd\u5728BaseClass\u4e2d\u5b9a\u4e49\u4e86\n    \/\/ \u4f46\u662f\u6211\u4eec\u7528extend\u66ff\u4ee3\u4e86implements\n    \/\/ \u8fd9\u5c06\u8986\u76d6\u5728BaseClass\u4e2d\u7684\u5b9a\u4e49\n    definedVariable : &quot;Defined in ImplementingClass&quot;,\n    testFunction : function(){\n        alert('This function is also defined in BaseClass');\n    }\n});\n \nvar demo_four = function(){\n    \/\/ \u521b\u5efa\u4e00\u4e2aImplementingClass\u5b9e\u4f8b\n    var test_class = new ExtendingClass('this is the input value');\n \n    \/\/ \u8c03\u7528testFunction()\uff08\u540c\u65f6\u5728BaseClass\u548cExtendingClass\u4e2d\u90fd\u6709\u5b9a\u4e49\uff09\n    test_class.testFunction();\n \n    \/\/ \u663e\u793a\u53d8\u91cfdefinedVariable\u7684\u503c\uff08\u540c\u65f6\u5728BaseClass\u548cExtendingClass\u4e2d\u90fd\u6709\u5b9a\u4e49\uff09\n    alert('test_class.definedVariable : ' + test_class.definedVariable);\n}\n<\/pre>\n<\/p>\n<p>\n<button id=\"demo_four_button\">demo_four()<\/button>\n<\/p>\n<p>\u4f7f\u7528extends\u7684\u53e6\u5916\u4e00\u4e2a\u6709\u7528\u7684\u529f\u80fd\u662f\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u529f\u80fd\uff1a\u5728\u8986\u76d6\u57fa\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u65f6\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5728\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8fd9\u6837\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff1a<\/p>\n<p><pre lang=\"javascript\">\ninitialize : function(){\n    alert('base class');\n}\n<\/pre>\n<\/p>\n<p>\u7136\u540e\uff0c\u5728\u6269\u5c55\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e0b\u9762\u8fd9\u6837\u4e00\u4e2a\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u90a3\u4e48\u5c06\u4f1a\u5f39\u51fa\u4e24\u4e2a\u63d0\u793a\u5206\u522b\u663e\u793a\u201cbase class\u201d\u548c\u201cextending class\u201d\u3002<\/p>\n<p><pre lang=\"javascript\">\ninitialize : function(){\n    \/\/ \u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\n    this.parent();\n    alert('extending class');\n}\n<\/pre>\n<\/p>\n<p>\u5982\u679c\u7236\u7c7b\u7684\u521d\u59cb\u5316\u51fd\u6570\u9700\u8981\u53c2\u6570\uff0c\u8bf7\u4e00\u5b9a\u8981\u786e\u4fdd\u5728\u8fd9\u4e2a\u521d\u59cb\u5316\u51fd\u6570\u4e2d\u53c8\u76f8\u540c\u7684\u8f93\u5165\u53c2\u6570\u5e76\u4f20\u9012\u7ed9\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u8bf7\u6ce8\u610f\u6211\u4eec\u6ca1\u6709\u7ed9input\u6307\u5b9a\u4efb\u4f55\u503c\u2014\u2014\u6211\u4eec\u53ea\u662f\u628a\u5b83\u4f20\u9012\u7ed9\u4e86\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5b83\u4f1a\u81ea\u52a8\u7ba1\u7406\u597d\u7684\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar ExtendingClass = new Class({\n    \/\/ \u91cd\u590d\u4e00\u904d\uff1a\u6211\u4eec\u5728\u4f7f\u7528\u6269\u5c55\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5b9e\u73b0\n    Extends : BaseClass,\n    initialize: function(input){\n        \/\/ \u901a\u8fc7\u8c03\u7528this.parent\u6765\u6267\u884c\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\n        this.parent(input);\n        \/\/ \u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u5728\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u505a\u4e00\u4e9b\u5176\u4ed6\u7684\u4e8b\u60c5\n        \/\/ \u800c\u4e0d\u7528\u5b8c\u5168\u8986\u76d6\u7236\u7c7b\u7684\u65b9\u6cd5\n        this.otherVariable = &quot;Original Input Was : &quot; + input;\n    }\n});\n \nvar demo_five = function(){\n    \/\/ \u521b\u5efa\u6211\u4eec\u7684\u6269\u5c55\u7c7b\u5b9e\u4f8b\n    var test_class = new ExtendingClass('this is the input value');    \n \n    \/\/ \u8c03\u7528testFunction\n    test_class.testFunction();\n \n    \/\/ \u663e\u793a\u53d8\u91cfotherVariable\u7684\u503c\n    alert(&quot;test_class.otherVariable : &quot; + test_class.otherVariable);\n}\n<\/pre>\n<\/p>\n<p>\n<button id=\"demo_five_button\">demo_five()<\/button>\n<\/p>\n<h3>.implement()\u65b9\u6cd5<\/h3>\n<p>\u4f60\u4e0d\u4ec5\u53ef\u4ee5\u4f7f\u7528implements\u548cextends\u6765\u6269\u5c55\u4f60\u7684\u7c7b\u5b9a\u4e49\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u539f\u6709\u7684\u7c7b\u6765\u4e00\u6b21\u6dfb\u52a0\u4e00\u4e2a\u529f\u80fd\u3002\u5728\u4e0b\u9762\u7684\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u7c7b\uff08calculator\uff09\uff0c\u5b9a\u4e49\u8fd9\u4e2a\u7c7b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u7ed9\u4e86\u5b83\u4e00\u4e2a\u5bf9\u4e24\u4e2a\u6570\u5b57\u505a\u52a0\u6cd5\u8fd0\u7b97\u548c\u4e00\u4e2a\u505a\u51cf\u6cd5\u8fd0\u7b97\u7684\u529f\u80fd\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar Calculator = new Class({\n    \/\/ \u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u6307\u5b9a\u4e24\u4e2a\u6570\u5b57\n    initialize: function(first_number, second_number){\n        this.first  = first_number;\n        this.second = second_number;\n    },\n    \/\/ \u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u52a0\n    \/\/ \u5e76\u8fd4\u56de\u7ed3\u679c\n    add : function(){\n        result = this.first + this.second;\n        alert(result);\n    },\n    \/\/ \u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u51cf\n    \/\/ \u5e76\u8fd4\u56de\u7ed3\u679c\n    subtract : function(){\n        result = this.first - this.second;\n        alert(result);\n    }\n \n});\n<\/pre>\n<\/p>\n<p>\u5982\u679c\u4f60\u53ea\u662f\u8981\u5bf9\u6570\u5b57\u505a\u52a0\u6cd5\u6216\u8005\u51cf\u6cd5\u8fd0\u7b97\uff0c\u8fd9\u4e00\u5207\u770b\u8d77\u6765\u90fd\u5f88\u597d\uff0c\u4f46\u662f\uff0c\u5982\u679c\u4f60\u8981\u5bf9\u5b83\u4eec\u505a\u4e58\u6cd5\u600e\u4e48\u529e\u5462\uff1f\u4f7f\u7528.implement();\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u7c7b\u6dfb\u52a0\u4e00\u4e2a\u529f\u80fd\uff0c\u5c31\u50cf\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u53e6\u5916\u4e00\u4e2a\u4ee5Calculator\u7c7b\u4e3a\u57fa\u7c7b\u7684\u7c7b\u4e00\u6837\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar demo_six = function(){\n    \/\/ \u4e3aCalculator\u7c7b\u5b9e\u73b0\n    \/\/ \u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\n    Calculator.implement({\n        \/\/ \u628a\u4e24\u4e2a\u6570\u5b57\u76f8\u4e58\n        \/\/ \u5e76\u8fd4\u56de\u7ed3\u679c\n        multiply : function(){\n            result = this.first * this.second;\n            alert(result);\n        }\n    });    \n \n    \/\/ \u5efa\u7acb\u4e00\u4e2aCalculator\u7c7b\u5b9e\u4f8b\n    var myCalculator = new Calculator(100, 50);\n \n    \/\/ \u8c03\u7528multiply\u65b9\u6cd5\n    myCalculator.multiply();\n}\n<\/pre>\n<\/p>\n<p>\n<button id=\"demo_six_button\">demo_six()<\/button>\n<\/p>\n<p>\u5728\u7c7b\u6559\u7a0b\u7684\u7b2c\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86print_r\u8fd9\u4e2a\u51fd\u6570\u6765\u8c03\u8bd5javascript\u3002\u4f7f\u7528implement\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u5b83\u975e\u5e38\u8f7b\u677e\u5730\u6253\u5370\u51fa\u51fa\u4e00\u4e2a\u7c7b\u4e2d\u7684\u53d8\u91cf\u503c\uff0c\u53ea\u8981\u5728Calculator\u4e2d\u5b9e\u73b0\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u884c\u4e86\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar demo_seven = function(){\n    \/\/ \u4e3aCalculator\u7c7b\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\n    \/\/ \u7528\u6765\u6253\u5370\u8fd9\u4e2a\u7c7b\u91cc\u9762\u7684\u5185\u5bb9\n    Calculator.implement({\n        show_class : function(){\n            alert(print_r(this, true));\n        }\n    });\n \n    \/\/ \u5efa\u7acb\u4e00\u4e2aCalculator\u7c7b\u5b9e\u4f8b\n    var myCalculator = new Calculator(100, 50);\n \n    \/\/ \u663e\u793a\u7c7b\u7684\u8be6\u7ec6\u4fe1\u606f\n    myCalculator.show_class();\n}\n<\/pre>\n<\/p>\n<p>\n<button id=\"demo_seven_button\">demo_seven()<\/button>\n<\/p>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<p>\u867d\u7136\u5f88\u7b80\u6d01\uff0c\u4e0d\u8fc7\u8fd9\u5bf9\u4e8e\u76f8\u5bf9\u6bd4\u8f83\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u8fd9\u4e2a\u7c7b\u6765\u8bf4\u4e0d\u662f\u4e00\u4e2a\u7279\u522b\u6709\u7528\u7684\u529f\u80fd\u3002\u4f46\u662f\uff0c\u7531\u4e8eMooTools\u91cc\u9762\u7684\u5927\u591a\u6570\u5bf9\u8c61\u90fd\u662f\u7528\u76f8\u540c\u7684\u65b9\u5f0f\u5efa\u7acb\u7684\u7c7b\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u6269\u5145MooTools\u7684\u7c7b\uff0c\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5b9e\u73b0\u4e86\u4e00\u4e2a\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u663e\u793a\u4efb\u4f55\u4f60\u60f3\u770b\u7684HTML\u7684\u5185\u5bb9\u7ed3\u6784\u3002\u8fd9\u4e2a\u529f\u80fd\u73b0\u5728\u5c31\u88ab\u81ea\u52a8\u5730\u6dfb\u52a0\u5230\u4e86\u4efb\u4f55\u4e0e\u4f60\u4ea4\u4e92\u7684HTML\u5143\u7d20\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u7ed9\u4f60\u7684\u5143\u7d20\u6dfb\u52a0\u4e00\u4e2ashowStructure\u5143\u7d20\u7684\u5b8c\u6574\u63cf\u8ff0\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar demo_eight = function(){\n    Element.implement({\n        showStructure : function(){\n            var structure = '&lt;pre&gt;' + print_r(this, true) + '&lt;\/pre&gt;';\n            \/\/ \u6253\u5f00\u4e00\u4e2a\u5f39\u51fa\u7a97\u53e3\n            newWindow = window.open('','Element Debug','height=600,width=600,scrollbars=yes');\n            \/\/ \u628a\u5185\u5bb9\u5199\u5165\u5230\u5f39\u51fa\u7a97\u53e3\u4e2d\n            newWindow.document.write(structure);\n        }\n    });\n \n    $('demo_eight').showStructure();\n}\n<\/pre>\n<\/p>\n<p><b>\u6ce8\u610f\uff1a<\/b>\u8981\u8ba9\u8fd9\u4e2a\u793a\u4f8b\u80fd\u6b63\u786e\u663e\u793a\uff0c\u4f60\u9700\u8981\u5148\u5141\u8bb8\u8be5\u9875\u9762\u5f39\u51fa\u7a97\u53e3\u3002<\/p>\n<p>\n<button id=\"demo_eight_button\">demo_eight()<\/button>\n<\/p>\n<h3>\u66f4\u591a\u5b66\u4e60<\/h3>\n<p><b><a href=\"http:\/\/www.consideropen.com\/downloads\/30days_of_moo\/mootorial_day21_classes_2.zip\" target=\"_blank\" title=\"\u4e0b\u8f7d\u4e00\u4e2a\u5305\u542b\u4f60\u5f00\u59cb\u6240\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f\u7684zip\u5305\">\u4e0b\u8f7d\u4e00\u4e2a\u5305\u542b\u4f60\u5f00\u59cb\u6240\u9700\u8981\u7684\u6240\u6709\u4e1c\u897f\u7684zip\u5305<\/a><\/b><\/p>\n<p><a href=\"http:\/\/mootools.net\/docs\/Class\/Class\" target=\"_blank\" title=\"MooTools Class\u6587\u6863\">MooTools Class\u6587\u6863<\/a><\/p>\n<p><a href=\"http:\/\/www.mootorial.com\/wiki\/mootorial\/02-class\" target=\"_blank\" title=\"\u4e00\u4e9b\u975e\u5e38\u597d\u7684\u5173\u4e8e\u66f4\u597d\u5730\u5229\u7528MooTools\u7c7b\u7684\u8ba8\u8bba\">\u4e00\u4e9b\u975e\u5e38\u597d\u7684\u5173\u4e8e\u66f4\u597d\u5730\u5229\u7528MooTools\u7c7b\u7684\u8ba8\u8bba<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1a30 Days of Mootools 1.2 Tutorials &#8211; Day 21 &#8211; Classes part II \u7c7b\u2014\u2014\u7b2c\u4e8c\u90e8\u5206 \u8bf7\u5c0a\u91cd\u4e2a\u4eba\u52b3\u52a8\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttp:\/\/fdream.net\uff0c\u8bd1\u8005\uff1aFdream \u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u51c6\u5907\u597d\u5f00\u59cb\u8fd9\u4e00\u8bb2\uff0c\u8bf7\u53c2\u8003\u8fd9\u4e00\u7cfb\u5217\u7684\u6559\u7a0b\uff0c\u8fd9\u91cc\u662f\u300aMooTools 1.2\u7cfb\u5217\u6559\u7a0b\u76ee\u5f55\u300b\u3002 Fdream\u6ce8\uff1a\u539f\u6587\u597d\u50cf\u5c11\u4e86\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u6211\u81ea\u4f5c\u4e3b\u5f20\u5730\u8865\u5145\u4e86\u4e0b\u9762\u8fd9\u4e00\u6bb5\u53caBaseClass\u7684\u4ee3\u7801\u3002 \u5728\u4eca\u5929\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u5b66\u4e60\u4e00\u4e0bMooTools\u4e2d\u7c7b\u7684\u5b9e\u73b0\u548c\u7ee7\u627f\uff08\u6269\u5c55\uff09\u3002\u901a\u8fc7\u5b9e\u73b0\u548c\u7ee7\u627f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u4f7f\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u518d\u91cd\u65b0\u58f0\u660e\u548c\u5b9e\u73b0\u76f8\u540c\u7684\u65b9\u6cd5\u3002MooTools\u4e2d\u7684Class\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e00\u4e2a\u57fa\u7c7b\uff08base class\uff09\u3002 var BaseClass = new Class({ \/\/ \u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5testFunction \/\/ \u8fd9\u4e2a\u65b9\u6cd5\u5f39\u51fa\u4e00\u4e2a\u5bf9\u8bdd\u6846 testFunction : function(){ alert(&#8216;This function is defined in BaseClass&#8217;); } }); \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a\u57fa\u7c7b\uff08base class\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u7c7b\u6765\u5b9e\u73b0\u5b83\u4ece\u800c\u4f7f\u7528\u5b83\u7684\u5168\u90e8\u529f\u80fd\u3002\u6ce8\u610f\uff0c\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7684\u65b0\u7c7b\u6ca1\u6709\u505a\u4efb\u4f55\u4e8b\u60c5\uff0c\u800c\u53ea\u662f\u5b9e\u73b0\u4e86\u57fa\u7c7bBaseClass\u3002 \/\/ \u521b\u5efa\u4e00\u4e2a\u540d\u53ebImplementingClass\u7684\u7c7b var ImplementingClass = new Class({ \/\/ \u6211\u6240\u505a\u7684\u5168\u90e8\u4e8b\u60c5\u5c31\u662f\u5b9e\u73b0Baseclass Implements : BaseClass }); &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[120,104,328,123],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-coding","tag-AJAX","tag-JavaScript","tag-mootools","tag-123"],"views":7808,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}