`
erichua
  • 浏览: 514435 次
  • 性别: Icon_minigender_2
  • 来自: 远方
社区版块
存档分类
最新评论

JQuery读书笔记--OOB Jquery class

阅读更多
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.
 
// 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);
});
 

分享到:
评论

相关推荐

    SATA学习笔记

    SATA学习笔记 SATA(Serial Advanced Technology Attachment)是一种高速串行接口技术,用于连接计算机和存储设备,如硬盘、固态硬盘、U盘等。下面是SATA学习笔记的详细知识点: 硬盘基本概念 硬盘是计算机系统的...

    Silverlight 客戶端桌面模式(OOB 模式)

    **Silverlight 客户端桌面模式(OOB模式)详解** Silverlight,作为微软开发的一种富互联网应用程序(RIA)技术,允许开发者创建交互性强、视觉效果丰富的网络应用。在 Silverlight 的众多特性中,"Out-of-Browser...

    ZedBoard_OOB_Design

    其中包含BOOT.BIN,devicetree_ramdisk.dtb,zImage。

    Python-230OOB一个通过FTP实现文件读取的python脚本

    在230-OOB项目中,可能包含了更复杂的逻辑,如错误处理、多文件操作等。项目名"230OOB"可能是指FTP服务器响应代码230,它表示用户已成功登录。"master"分支通常指的是版本控制系统的主分支,代表了项目的最新稳定...

    nfc-to-bluetooth:点对点 NFC 到蓝牙 OOB

    《NFC到蓝牙OOB:点对点连接技术解析》 在现代移动设备通信领域,NFC(近场通信)和蓝牙技术的应用日益广泛。本文将深入探讨如何利用NFC进行蓝牙的点对点(Out-Of-Band,OOB)配对,特别是在多人游戏等应用场景中的...

    林轩田《机器学习技法》课程笔记11 -- Gradient Boosted Decision Tree1

    【机器学习技法】课程笔记11 -- Gradient Boosted Decision Tree1 本次课程主要探讨了Adaptive Boosting(AdaBoost)算法在决策树模型中的应用,特别是如何将权重引入以优化决策树算法,以及如何通过Gradient ...

    Silverlight OOB应用源码

    **Silverlight OOB应用源码详解** Silverlight是微软推出的一种富互联网应用程序(RIA)平台,它允许开发者创建具有丰富视觉效果和交互性的应用程序,这些应用程序可以在网页上运行,也可以以“Out-of-Browser”...

    29-oob.rar

    网络编程从大的方面说就是对信息的发送到接收,中间传输为物理线路的作用。网络编程最主要的工作就是在发送端把信息通过规定好的协议进行组装包,在接收端按照规定好的协议把包进行解析,从而提取出对应的信息,达到...

    OOB modem 配置文档

    在解读给定的文档信息后,我们可以提炼出以下与OOB modem配置相关的知识点: 1. OOB Modem概念: OOB(Out-of-Band)Modem是一种用于远程管理网络设备的通信工具。当网络出现故障或无法通过局域网进行远程访问时,...

    OOB v10 绿色

    Oracle Optimizer Out-of-Browser (OOB) 是一个专门针对Oracle数据库进行管理和优化的高效工具。这个工具的独特之处在于它的“绿色”特性,意味着它无需安装即可使用,这对于那些希望快速部署、不想在系统上留下额外...

    CC3200 LaunchPad使用入门03-OOB例程

    ### CC3200 LaunchPad使用入门03-OOB例程详解 #### 实验概述 本实验旨在介绍如何使用CC3200 LaunchPad进行OutOfBox (OOB) 示例操作,这是一种开箱即用的功能,允许用户无需额外编程即可体验CC3200芯片的强大功能。...

    Oracle的客户端工具OOB7

    Oracle的客户端工具OOB7(Oracle Application Express Optimizer)是Oracle公司提供的一款高效、易用的数据库管理与开发工具,相较于PL/SQL Developer等其他客户端工具,它在使用体验上有着显著的优势。本文将深入...

    oracle管理软件OOB

    Oracle企业管理器(Oracle Enterprise Manager, OOB)是Oracle公司提供的一款强大的数据库管理和监控工具,用于高效、全面地管理Oracle数据库环境。"OOB"可能是"Out of the Box"的缩写,暗示这是一个开箱即用的解决...

    Silverlight OOB Demo

    **Silverlight OOB(Out-of-Browser)Demo** Silverlight是一种由Microsoft开发的富互联网应用程序(RIA)平台,它允许开发者创建具有丰富图形、动画和交互性的Web应用。Silverlight OOB(Out-of-Browser)功能是...

    oob.rar_OOB_TCP client

    OOB(Out-of-Band Data)在TCP/IP协议中是一个重要的概念,主要涉及到TCP协议的紧急数据处理。OOB数据通常被用来传输特殊或者优先级较高的信息,比如网络控制消息。本项目“oob.rar_OOB_TCP client”提供了一个...

    使用Silverlight5 在vs2010平台上设计OOB模拟浏览器访问网页

    本项目主要探讨的是如何在Visual Studio 2010中利用Silverlight 5创建一个Out-of-Browser(OOB)应用程序,这种应用程序可以在用户计算机上独立运行,而不是局限于浏览器环境。OOB模式为Silverlight应用提供了更大的...

    OOB9.0_oracle数据库操作工具

    "OOB9.0_oracle数据库操作工具"则是一款专为Oracle数据库设计的客户端工具,它旨在简化数据库的管理和维护工作,提供友好的用户界面和高效的数据库操作功能。 OOB9.0(可能代表Oracle Object Browser的版本9.0)...

    oob-vim:Zer4tul的vim配置

    oob-vim是Vim, , 和一组配置。 它使Vim可以在类似Unix的系统中使用,甚至可以在Windows(未经测试)上使用。 安装 手动安装 从github克隆Git Repo git clone git://github.com/zer4tul/oob-vim.git ~/.oob-vim ...

    Silverlight OOB.rar

    **Silverlight OOB技术详解** Silverlight是一款由微软开发的富互联网应用程序(RIA)平台,它为Web开发者提供了丰富的媒体体验和交互式用户界面设计工具。"OOB"是"Out-Of-Browser"的缩写,是Silverlight的一个重要...

    Silerlight OOB 导出到Excel(2007up)

    在本文中,我们将深入探讨如何在Silverlight Out-of-Browser (OOB) 应用程序中实现数据导出到Excel 2007及更高版本的功能。Silverlight是一种由Microsoft开发的富客户端技术,用于创建交互式的Web应用程序,而OOB...

Global site tag (gtag.js) - Google Analytics