首页 > 代码库 > 日常捕获的野生知识 - javascript 与 类

日常捕获的野生知识 - javascript 与 类

  javascript 并不提供类这样方便实用的面向对象类型,今天学习到了,直接上代码:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title> Demo Js_Class </title><script type="text/javascript" charset="utf-8">         var Student = {      CreateNew : function(){      var student = {};      student.name = "学生";      var pte = "我是私有的";      tudent.pte = function(){return pte;}      student.showname = function(){return "我是"+student.name;}      return student;   } }; var HighSchool = {      CreateNew : function(){      var h_stu = Student.CreateNew();      h_stu.scole = "100";      h_stu.showscole = function(){return "我的分数"+h_stu.scole;}      return h_stu;   } };window.onload = function(){      var high = HighSchool.CreateNew();      high.name = "小莫";      var str = high.pte()+"\n"+high.showname()+"\n"+high.showscole();      document.write(str);}</script></head><body></body></html>

  这叫什么极简主义法,还有好多其他的实现方法,我就不多说了,给大家一个连接:
http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html