惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
腾讯CDC
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threatpost
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
B
Blog RSS Feed
Forbes - Security
Forbes - Security
P
Privacy & Cybersecurity Law Blog
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
T
Tenable Blog
MyScale Blog
MyScale Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
W
WeLiveSecurity
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
罗磊的独立博客
The Last Watchdog
The Last Watchdog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
美团技术团队
Microsoft Security Blog
Microsoft Security Blog

博客园 - feenan

AngularJS 源码分析3 AngularJS 源码分析2 AngularJS 源码分析1 javascript 中关于对象转换数字值的一些特点 转 CSS hack:针对IE6,IE7,firefox显示不同效果 CSS中一些不经意的细节问题1 CSS关于元素垂直居中的问题 (转)雅虎WEB前端网站优化 -- 34条军规 一个比较常用的关于php下的mysql数据操作类 Js中的一个日期处理格式化函数 Underscore.js 1.3.3 源码分析收藏 Backbone.js 0.9.2 源码分析收藏 Nodejs实现的一个静态服务器例子 Javascript MVC学习杂记3 Javascript MVC学习杂记2 C语言实现的一个简单的HTTP程序 C语言string.h中常用字符函数介绍 通过日期生成星期几 Javascript 模块化编程
Javascript MVC学习杂记1
feenan · 2013-05-04 · via 博客园 - feenan

 这两天看了下<基于MVC的Javascript 富应用开发>,感觉刚开始讲的那个model类,比较有趣,所以自己就造了一个轮子,体会了下,当然也参考了点代码,见下面的代码

//基于原型的继承
if(typeof Object.create!=="function"){
   Object.create=function(o){
      function F(){}
	  F.prototype=o;
	  return new F();
   }
}
var Model={
   prototype:{
      init:function(){
		console.log('Model.prototype.init');
      },
	  find:function(){
	    console.log('Model.prototype.find');
	  }
   },
   inherited:function(){
      //console.log('exec inherited')
   },
   created:function(){
      //console.log('exec created!');
   },
   create:function(){
      var object=Object.create(this);
	  object.parent=this;
	  object.prototype=object.fn=Object.create(this.prototype);

	  object.created();//创建完成方法

	  this.inherited(object); //继承父类属性方法

	  return object;
   },
   init:function(){
      var instance=Object.create(this.prototype);
	  instance.parent=this;
	  instance.init.apply(instance,arguments);
	  return instance;
   },
   extend:function(o){
      for(var key in o){
	    this[key]=o[key];
	  }
   },
   include:function(o){
      for(var key in o){
	     this.prototype[key]=o[key];
	  }
   }
};

var User=Model.create();
//类方法
User.extend({
   initattr:function(o){
      var obj=this.init();
	  for(var key in o){
	    obj[key]=o[key];
	  }
	  return obj;
   }
});
//类实例方法
User.include({
   getname:function(){
      console.log(this.name);
   }
});
var user=User.initattr({"name":"xuwm","age":12});
user.getname();

把上面的代码保存到demo.js文件里,然后可以直接在NODE里运行,命令行模式下,node demo.js