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

推荐订阅源

C
Check Point Blog
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
U
Unit 42
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
美团技术团队
雷峰网
雷峰网
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
罗磊的独立博客
MyScale Blog
MyScale Blog
博客园_首页
IT之家
IT之家
F
Fortinet All Blogs
博客园 - Franky

博客园 - Jacken

ios basics 关于 php mvc 转: Basic JavaScript Part 8: Namespaces Simple JavaScript Inheritance 转: CSS网页布局教程:绝对定位和相对定位 js 类与对象 div 垂直居中 布局 boost signal 用法与用处... Flash嵌入纯Win32程序 及 事件接收 消息发送器设计 工若善其器,必然利其事。 HTML5开发工具选择 项目 "差不多成功? 简直就是失败". html css 布局 ubuntu 使用 wifi 连接上网 用背景图片填充Edit控件... 游戏类初步一.. C++的异常处理方法之一. - Jacken - 博客园 C++ EventHandler v0.02 在命令行中使用cl工具生成纯资源的DLL文件...
javascript 命名空间 继承 实现
Jacken · 2012-04-20 · via 博客园 - Jacken

javascript 命名空间的写法及实现方式: 

var collections;
if (!collections) collections = {};
collections.sets = {};
(function namespace() {
// ... Lots of code omitted...
//
 Now export our public API to the namespace object created above
collections.sets.AbstractSet = AbstractSet;
collections.sets.NotSet = NotSet; // And so on...
//
 No return statement is needed since exports were done above.
}());

javascript 继承实现方式:

混合方式

这种继承方式使用构造函数定义类,并非使用任何原型。对象冒充的主要问题是必须使用构造函数方式,这不是最好的选择。不过如果使用原型链,就无法使用带参数的构造函数了。开发者如何选择呢?答案很简单,两者都用。

在前一章,我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。这种方式同样适用于继承机制,用对象冒充继承构造函数的属性,用原型链继承 prototype 对象的方法。用这两种方式重写前面的例子,代码如下:

function ClassA(sColor) {
    this.color = sColor;
}

ClassA.prototype.sayColor = function () {
    alert(this.color);
};

function ClassB(sColor, sName) {
    ClassA.call(this, sColor);
    this.name = sName;
}

ClassB.prototype = new ClassA();

ClassB.prototype.sayName = function () {
    alert(this.name);
};