- 浏览: 518899 次
- 性别:
- 来自: 远方
文章分类
最新评论
-
futoviny:
挺有用的 javax.xml.ws.Holder
CXF学习笔记---让通过参数传递数据 -
zqb666kkk:
能提供下示例demo吗
CXF学习笔记---给CXF加一把锁WS_SECURITY应用 -
hello_world_wdq:
真是坑爹,这能用吗,害我半天时间浪费了
Extjs学习总结---RowExpander 的异步调用 -
ubuntu的疯狂:
第一段代码怎么用????求解释!!弄了很久还是不得结果……
Extjs学习总结---RowExpander 的异步调用 -
107x:
不错,谢谢分享!
[log4j]Slf4j的包冲突
It's really exiting, Jquery can compose in oo way. if I have time,I'll study it literally.
Home
The Class Class
At Digg we use jQuery extensively, but it doesn’t offer a much in the way of Object-Oriented !JavaScript. Existing OOJS libraries weren’t a good fit - often trying to shoehorn traditional OOP patterns in where they don’t quite fit - so we rolled our own.
Enjoy!
Part 1: Creating and Manipulating Classes and Namespaces
Hello World
Simplest usage, creates a dynamic class in the current scope
var MyClass = Class.create();
var mc = new MyClass();
Static / Singleton
And now, a static class. Anytime the last argument passed to Class.create is boolean, it’ll serve as the static switch (defaults to false).
var MyClass = Class.create(true);
MyClass.namespace('foo');
Adding the Constructor and Methods
Ok, so we have classes now, but their constructors and prototypes are empty. We could use $.extend, but why not make it a bit tidier? You can pass any number of objects to Class.create and they’ll be tacked onto your class’ prototype (dynamic) or the class itself (static). The ‘init’ method serves as each Class’ constructor.
var method = {
someFunc: function(){}
}
var MyClass = Class.create({
init: function(){
console.log('You instantiated a Class!');
},
myFunc: function(){},
myProp: 'foo'
}, method);
var foo = new MyClass();
foo.someFunc();
Namespaces
Alright, so we have classes now, and they have methods. What’s next? Namespaces. Class.namespace is a badass object manipulation method. If you pass it a string, it’ll turn that string into a static class inside the current one.
var MyClass = Class.create(true);
//this...
MyClass.namespace('bar');
//...is the same as this
$.extend(MyClass, {
bar: Class.create(true);
});
Deep Namespaces
But wait, there’s more. You can pass in a dot-notated String for more fun. Note: existing properties will not be overwritten. If !MyClass.foo exists, !MyClass.namespace(‘foo.bar’) will keep foo as is and copy bar into it, though you should really use !MyClass.foo.namespace(‘bar’) at that point.
var MyClass = Class.create(true);
//this...
MyClass.namespace('bar.baz.quux');
//...is the same as this
$.extend(MyClass, {
bar: Class.create(true);
});
$.extend(MyClass.bar, {
baz: Class.create(true);
});
$.extend(MyClass.bar.baz, {
quux: Class.create(true);
});
Existing Global Namespaces
And more: If a namespace already exists as an object in the global scope, it’ll be copied into your class. This is handy if you have an existing object or class sitting around and you want to integrate it into your obsessive/compulsive organization scheme.
var MyClass = Class.create(true);
var stuff = Class.create({
things: function(){}
}, true);
MyClass.namespace('bar.stuff');
//MyClass.bar.stuff.things.constructor == Function
Namespace / Method Combo Object
But wait, ’’there’s more’’: Pass in an object and it’ll run through and copy its properties that are Objects or Functions into your Class.
var MyClass = Class.create(true);
var stuff = Class.create({
things: function(){}
}, true);
MyClass.namespace({
stuff: {
things: function(){}
}
});
//MyClass.stuff.things.constructor == Function
Multiple Namespaces With an Array
Want to add multiple namespaces at the same level? Pass in an array of strings or objects:
var MyClass = Class.create(true);
MyClass.namespace([
'foo.bar',
{
baz: {
doStuff: function(){}
}
},
'quux'
]);
Dynamic Classes in Namespaces
Phew. Ok then. So we have Classes, they have methods and namespaces (which are, by default, static classes). Next up: Dynamic classes inside namespaces. For this we’ll use the version of Class.create that gets prototyped onto all classes (so !MyClass.create, not Class.create). It differs from the main one in that you pass it a name for the inner Class before all of the regular arguments, like so:
var MyClass = Class.create(true);
MyClass.create('MyOtherClass', {
myOtherFunc: function(){}
});
var foo = new MyClass.MyOtherClass();
foo.myOtherFunc();
The Terrible Secret of Space
Let’s put it all together for one last demo:
var D = Class.create(true);
D.namespace('estroy.humans');
D.estroy.humans.create('Now', {
rawr: function() {
console.log('BOOM! SMASH! POW!');
}
});
var foo = new D.estroy.humans.Now();
foo.rawr();
Part 2: Extending Classes
First, let’s talk about what happens when you extend a class.
var Foo = Class.create({
fooz: function() {
alert("Foo's fooz was called");
}
});
var Bar = Class.create(Foo.prototype, {
fooz: function() {
alert("Bar's fooz was called");
}
});
You could say that we just extended Foo with Bar, and for convenience’s sake we’ll refer to this process as “extending” a class, but that’s not quite what’s really going on. What we’ve done here is we’ve created the class Bar with a copy of Foo’s prototype, and overwritten Foo.prototype.fooz.
Remember, JavaScript doesn’t use classical inheritance. We have no formal classes, nor subs, nor supers. What we have is Objects, their prototypes and a perverse need to wrangle them into a pattern that resembles normal OOP. For that reason, when Class sees a duplicate property whose constructor is Function, it will copy that method into an object called ‘supers’ before overwriting it.
The effect of that copy is this:
var Foo = Class.create({
fooz: function() {
alert("Foo's fooz was called");
}
});
var Bar = Class.create(Foo.prototype, {
fooz: function() {
this.sup();
alert("Bar's fooz was called");
}
});
The call above to “this.sup()” (The word ‘super’ wasn’t used because it’s reserved in JS 2.0) invokes the copy of Foo.prototype.fooz that was stored in Bar.prototype.supers. Calling Bar.prototype.fooz() will now trigger both Foo and Bar’s alerts. What we have here, in essence, is a very simple connection between a sub-class and its super.
It comes with a significant caveat though: It only works one level above the current class, each time you extend a class and overwrite one of its methods, only the last duplicate method is stored in the “supers” object.
Consider the following:
var Foo = Class.create({
fooz: function() {
console.log('fooz of Foo');
}
});
var Bar = Class.create(Foo.prototype, {
fooz: function() {
this.sup();
console.log('fooz of Bar');
}
});
var Baz = Class.create(Bar.prototype, {
fooz: function() {
this.sup();
console.log('fooz of Baz');
}
});
Baz.prototype.supers.fooz now contains code that invokes “this.sup”, which is a reference to Baz.prototype.supers.fooz. Enjoy your beachball!
In the future we may add support for inheriting as far down as you’d care to go, but for now this single level adds as much classical inheritance as we’d care to see in !JavaScript.
Home
The Class Class
At Digg we use jQuery extensively, but it doesn’t offer a much in the way of Object-Oriented !JavaScript. Existing OOJS libraries weren’t a good fit - often trying to shoehorn traditional OOP patterns in where they don’t quite fit - so we rolled our own.
Enjoy!
Part 1: Creating and Manipulating Classes and Namespaces
Hello World
Simplest usage, creates a dynamic class in the current scope
var MyClass = Class.create();
var mc = new MyClass();
Static / Singleton
And now, a static class. Anytime the last argument passed to Class.create is boolean, it’ll serve as the static switch (defaults to false).
var MyClass = Class.create(true);
MyClass.namespace('foo');
Adding the Constructor and Methods
Ok, so we have classes now, but their constructors and prototypes are empty. We could use $.extend, but why not make it a bit tidier? You can pass any number of objects to Class.create and they’ll be tacked onto your class’ prototype (dynamic) or the class itself (static). The ‘init’ method serves as each Class’ constructor.
var method = {
someFunc: function(){}
}
var MyClass = Class.create({
init: function(){
console.log('You instantiated a Class!');
},
myFunc: function(){},
myProp: 'foo'
}, method);
var foo = new MyClass();
foo.someFunc();
Namespaces
Alright, so we have classes now, and they have methods. What’s next? Namespaces. Class.namespace is a badass object manipulation method. If you pass it a string, it’ll turn that string into a static class inside the current one.
var MyClass = Class.create(true);
//this...
MyClass.namespace('bar');
//...is the same as this
$.extend(MyClass, {
bar: Class.create(true);
});
Deep Namespaces
But wait, there’s more. You can pass in a dot-notated String for more fun. Note: existing properties will not be overwritten. If !MyClass.foo exists, !MyClass.namespace(‘foo.bar’) will keep foo as is and copy bar into it, though you should really use !MyClass.foo.namespace(‘bar’) at that point.
var MyClass = Class.create(true);
//this...
MyClass.namespace('bar.baz.quux');
//...is the same as this
$.extend(MyClass, {
bar: Class.create(true);
});
$.extend(MyClass.bar, {
baz: Class.create(true);
});
$.extend(MyClass.bar.baz, {
quux: Class.create(true);
});
Existing Global Namespaces
And more: If a namespace already exists as an object in the global scope, it’ll be copied into your class. This is handy if you have an existing object or class sitting around and you want to integrate it into your obsessive/compulsive organization scheme.
var MyClass = Class.create(true);
var stuff = Class.create({
things: function(){}
}, true);
MyClass.namespace('bar.stuff');
//MyClass.bar.stuff.things.constructor == Function
Namespace / Method Combo Object
But wait, ’’there’s more’’: Pass in an object and it’ll run through and copy its properties that are Objects or Functions into your Class.
var MyClass = Class.create(true);
var stuff = Class.create({
things: function(){}
}, true);
MyClass.namespace({
stuff: {
things: function(){}
}
});
//MyClass.stuff.things.constructor == Function
Multiple Namespaces With an Array
Want to add multiple namespaces at the same level? Pass in an array of strings or objects:
var MyClass = Class.create(true);
MyClass.namespace([
'foo.bar',
{
baz: {
doStuff: function(){}
}
},
'quux'
]);
Dynamic Classes in Namespaces
Phew. Ok then. So we have Classes, they have methods and namespaces (which are, by default, static classes). Next up: Dynamic classes inside namespaces. For this we’ll use the version of Class.create that gets prototyped onto all classes (so !MyClass.create, not Class.create). It differs from the main one in that you pass it a name for the inner Class before all of the regular arguments, like so:
var MyClass = Class.create(true);
MyClass.create('MyOtherClass', {
myOtherFunc: function(){}
});
var foo = new MyClass.MyOtherClass();
foo.myOtherFunc();
The Terrible Secret of Space
Let’s put it all together for one last demo:
var D = Class.create(true);
D.namespace('estroy.humans');
D.estroy.humans.create('Now', {
rawr: function() {
console.log('BOOM! SMASH! POW!');
}
});
var foo = new D.estroy.humans.Now();
foo.rawr();
Part 2: Extending Classes
First, let’s talk about what happens when you extend a class.
var Foo = Class.create({
fooz: function() {
alert("Foo's fooz was called");
}
});
var Bar = Class.create(Foo.prototype, {
fooz: function() {
alert("Bar's fooz was called");
}
});
You could say that we just extended Foo with Bar, and for convenience’s sake we’ll refer to this process as “extending” a class, but that’s not quite what’s really going on. What we’ve done here is we’ve created the class Bar with a copy of Foo’s prototype, and overwritten Foo.prototype.fooz.
Remember, JavaScript doesn’t use classical inheritance. We have no formal classes, nor subs, nor supers. What we have is Objects, their prototypes and a perverse need to wrangle them into a pattern that resembles normal OOP. For that reason, when Class sees a duplicate property whose constructor is Function, it will copy that method into an object called ‘supers’ before overwriting it.
The effect of that copy is this:
var Foo = Class.create({
fooz: function() {
alert("Foo's fooz was called");
}
});
var Bar = Class.create(Foo.prototype, {
fooz: function() {
this.sup();
alert("Bar's fooz was called");
}
});
The call above to “this.sup()” (The word ‘super’ wasn’t used because it’s reserved in JS 2.0) invokes the copy of Foo.prototype.fooz that was stored in Bar.prototype.supers. Calling Bar.prototype.fooz() will now trigger both Foo and Bar’s alerts. What we have here, in essence, is a very simple connection between a sub-class and its super.
It comes with a significant caveat though: It only works one level above the current class, each time you extend a class and overwrite one of its methods, only the last duplicate method is stored in the “supers” object.
Consider the following:
var Foo = Class.create({
fooz: function() {
console.log('fooz of Foo');
}
});
var Bar = Class.create(Foo.prototype, {
fooz: function() {
this.sup();
console.log('fooz of Bar');
}
});
var Baz = Class.create(Bar.prototype, {
fooz: function() {
this.sup();
console.log('fooz of Baz');
}
});
Baz.prototype.supers.fooz now contains code that invokes “this.sup”, which is a reference to Baz.prototype.supers.fooz. Enjoy your beachball!
In the future we may add support for inheriting as far down as you’d care to go, but for now this single level adds as much classical inheritance as we’d care to see in !JavaScript.
// Hello World var MyClass = Class.create(); var mc = new MyClass(); // And now, a static class. Anytime the last argument passed to Class.create is boolean, it’ll serve as the static switch (defaults to false). var MyClass = Class.create(true); MyClass.namespace('foo'); // Add constructor and methods var method = { someFunc: function(){} } var MyClass = Class.create({ init: function(){ console.log('You instantiated a Class!'); }, myFunc: function(){}, myProp: 'foo' }, method); var foo = new MyClass(); foo.someFunc(); // Namespaces var MyClass = Class.create(true); //this... MyClass.namespace('bar'); //...is the same as this $.extend(MyClass, { bar: Class.create(true); });
发表评论
-
jQuery插件---兼容IE6的固定悬浮Postion:Fixed
2011-02-19 08:04 6228制作一个兼容IE6中的Postion:Fixed固定悬浮效果, ... -
JQuery读书笔记--Ajax调用
2009-09-24 05:31 1403Jquery中Ajax调用 $.ajax({ ... -
JQuery读书笔记--常用工具
2009-09-22 07:42 1080http://code.google.com/p/jqu ... -
JQuery读书笔记--JQuery1.3的Dreamweaver cs4的扩展
2009-04-22 03:22 2942发现专门为Dw4的jquery1.3扩展。 下载地 ... -
JQuery读书笔记--透明圆角插件
2009-04-16 08:54 1274将透明圆角功能做成了插件。其实很简单。 -
JQuery读书笔记----样例和最佳实践
2009-04-09 07:06 2054jQuery and JavaScript Coding: E ... -
JQuery实例----浮动login
2009-04-09 05:17 1959相当不错的浮动login,感觉css技巧多于js技巧。 -
JQuery读书笔记--10 Best jQuery Plugins - March 2009
2009-03-27 07:46 2123Ajax最新Jquery plugins的 ... -
JQuery读书笔记--Learning Jquery1.3
2009-03-25 08:15 1086The latest book of jquery 1.3. ... -
JaveScript读书笔记-----Js 的 OOP编程
2009-03-25 08:09 1927一本好书,系统地讲js oop的。早几年看到就好了。英文的哦 -
JQuery-flexgrid的新家
2009-03-03 04:27 2771Jquery flexgrid的原来网址好像一直进不去,据说是 ... -
JQuery读书笔记--JQuery库中的Type技巧总结-3
2009-02-26 09:20 16157.Options JQuery中大量的使用Options作 ... -
JQuery读书笔记--JQuery库中的Type技巧总结-2
2009-02-26 01:45 12814.boolean boolean只有二个值true ... -
JQuery读书笔记--JQuery库中的Type技巧总结-1
2009-02-24 08:18 2872Javascript中有自己的各种type以及伪类pseudo ... -
JQuery读书笔记---很全面的教程
2009-02-13 13:11 1190http://www.bennadel.com/resourc ... -
JQuery读书笔记--JQuery官方论坛
2009-01-29 04:24 1176终于出现了,JQuery正一步一步完善自己。虽然这个论坛个人感 ... -
JQuery读书笔记--JQuery的OOP编程模板
2009-01-29 04:21 2423// // create closure // (functi ... -
JQuery读书笔记--clientX, clientY,offsetX, offsetY的区别
2009-01-28 04:06 4677Html中定位是非常重要的,否则再好的东西不能在它应该在的地方 ... -
JQuery Sample--改写后的lessmore
2009-01-21 09:47 1290js (function($) { // public ... -
JQuery读书笔记--PLUGIN code模板
2009-01-21 06:49 1353自己总结的PLUGIN code模板 /** * @a ...
相关推荐
SATA学习笔记 SATA(Serial Advanced Technology Attachment)是一种高速串行接口技术,用于连接计算机和存储设备,如硬盘、固态硬盘、U盘等。下面是SATA学习笔记的详细知识点: 硬盘基本概念 硬盘是计算机系统的...
**Silverlight 客户端桌面模式(OOB模式)详解** Silverlight,作为微软开发的一种富互联网应用程序(RIA)技术,允许开发者创建交互性强、视觉效果丰富的网络应用。在 Silverlight 的众多特性中,"Out-of-Browser...
其中包含BOOT.BIN,devicetree_ramdisk.dtb,zImage。
在230-OOB项目中,可能包含了更复杂的逻辑,如错误处理、多文件操作等。项目名"230OOB"可能是指FTP服务器响应代码230,它表示用户已成功登录。"master"分支通常指的是版本控制系统的主分支,代表了项目的最新稳定...
《NFC到蓝牙OOB:点对点连接技术解析》 在现代移动设备通信领域,NFC(近场通信)和蓝牙技术的应用日益广泛。本文将深入探讨如何利用NFC进行蓝牙的点对点(Out-Of-Band,OOB)配对,特别是在多人游戏等应用场景中的...
【机器学习技法】课程笔记11 -- Gradient Boosted Decision Tree1 本次课程主要探讨了Adaptive Boosting(AdaBoost)算法在决策树模型中的应用,特别是如何将权重引入以优化决策树算法,以及如何通过Gradient ...
**Silverlight OOB应用源码详解** Silverlight是微软推出的一种富互联网应用程序(RIA)平台,它允许开发者创建具有丰富视觉效果和交互性的应用程序,这些应用程序可以在网页上运行,也可以以“Out-of-Browser”...
网络编程从大的方面说就是对信息的发送到接收,中间传输为物理线路的作用。网络编程最主要的工作就是在发送端把信息通过规定好的协议进行组装包,在接收端按照规定好的协议把包进行解析,从而提取出对应的信息,达到...
在解读给定的文档信息后,我们可以提炼出以下与OOB modem配置相关的知识点: 1. OOB Modem概念: OOB(Out-of-Band)Modem是一种用于远程管理网络设备的通信工具。当网络出现故障或无法通过局域网进行远程访问时,...
- Detect OOB and COMWAKE - Detect the K28.5 comma character and provide a 16 bit parallel output - Power management mode handled by state machine (shared between Phy and Link layer) - Provides error ...
Oracle Optimizer Out-of-Browser (OOB) 是一个专门针对Oracle数据库进行管理和优化的高效工具。这个工具的独特之处在于它的“绿色”特性,意味着它无需安装即可使用,这对于那些希望快速部署、不想在系统上留下额外...
Oracle企业管理器(Oracle Enterprise Manager, OOB)是Oracle公司提供的一款强大的数据库管理和监控工具,用于高效、全面地管理Oracle数据库环境。"OOB"可能是"Out of the Box"的缩写,暗示这是一个开箱即用的解决...
### CC3200 LaunchPad使用入门03-OOB例程详解 #### 实验概述 本实验旨在介绍如何使用CC3200 LaunchPad进行OutOfBox (OOB) 示例操作,这是一种开箱即用的功能,允许用户无需额外编程即可体验CC3200芯片的强大功能。...
Oracle的客户端工具OOB7(Oracle Application Express Optimizer)是Oracle公司提供的一款高效、易用的数据库管理与开发工具,相较于PL/SQL Developer等其他客户端工具,它在使用体验上有着显著的优势。本文将深入...
**Silverlight OOB(Out-of-Browser)Demo** Silverlight是一种由Microsoft开发的富互联网应用程序(RIA)平台,它允许开发者创建具有丰富图形、动画和交互性的Web应用。Silverlight OOB(Out-of-Browser)功能是...
OOB(Out-of-Band Data)在TCP/IP协议中是一个重要的概念,主要涉及到TCP协议的紧急数据处理。OOB数据通常被用来传输特殊或者优先级较高的信息,比如网络控制消息。本项目“oob.rar_OOB_TCP client”提供了一个...
本项目主要探讨的是如何在Visual Studio 2010中利用Silverlight 5创建一个Out-of-Browser(OOB)应用程序,这种应用程序可以在用户计算机上独立运行,而不是局限于浏览器环境。OOB模式为Silverlight应用提供了更大的...
"OOB9.0_oracle数据库操作工具"则是一款专为Oracle数据库设计的客户端工具,它旨在简化数据库的管理和维护工作,提供友好的用户界面和高效的数据库操作功能。 OOB9.0(可能代表Oracle Object Browser的版本9.0)...
oob-vim是Vim, , 和一组配置。 它使Vim可以在类似Unix的系统中使用,甚至可以在Windows(未经测试)上使用。 安装 手动安装 从github克隆Git Repo git clone git://github.com/zer4tul/oob-vim.git ~/.oob-vim ...
**Silverlight OOB技术详解** Silverlight是一款由微软开发的富互联网应用程序(RIA)平台,它为Web开发者提供了丰富的媒体体验和交互式用户界面设计工具。"OOB"是"Out-Of-Browser"的缩写,是Silverlight的一个重要...