IIssNan's Notes

Quick notes


  • 首页

  • 归档

  • 标签

  • 搜索

JavaScript中的单例模式(Singleton Pattern )

发表于 2013-08-01 | 分类于 技术水波文 | | 阅读次数

单例(Singleton)的定义:

The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was create the first time.单例模式是指在程序中一种特殊的类(class),他们仅有一个实例。这意味着当这个类在被第一次实例化后,之后的实例化操作都将获得到第一次实例化时产生的实例。

对象字面量与单例

以对象字面量(Object Literal)定义的对象本身即为单例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var o1 = {
name: "SP1 Object Literal",
getName: function() {
return this.name;
}
};
var o2 = {
name: "SP1 Object Literal",
getName: function() {
return this.name;
}
};
// o2的键值与o1完全相同,但他们是不同的对象。
console.log(o1 === o2); // false

对象字面量实现单例模式的另外一种形式:

阅读全文 »

Prevent VMWare WorkStation guest machines from sysning time with host

发表于 2013-07-25 | | 阅读次数
  1. Navigate to the directory where the virtual machine stored.
  2. Open %virtual machine name%.vmx file with your favourite editor.
  3. Search Text: “tools.syncTime”.
  4. Add the following text under the result line:

    time.synchronize.continue = "FALSE"
    time.synchronize.restore = "FALSE"
    time.synchronize.resume.disk = "FALSE"
    time.synchronize.shrink = "FALSE"
    time.synchronize.tools.startup = "FALSE"
    time.synchronize.tools.enable = "FALSE"
    time.synchronize.resume.host = "FALSE"
    

5.Restart the guest to check the result.

JavasScript 语言的 void 操作符

发表于 2013-04-10 | 分类于 技术水波文 | | 阅读次数

JavaScript 中的 void 操作符语法如:

1
void expression

void 执行 expression 并返回 undefined。

void 常见的两个用途是:

  1. 获取 undefined 变量
  2. 作为 href 的伪协议

在 JavaScript中 undefined 并不是一个保留字,所以存在着被更改的可能,这也意味着直接通过判断给定的变量是否 undefined 并非可靠。所以此时可以通过 void 来判断变量是否是 undefined:

1
2
3
4
// underscore 中的 isUndefined 函数
_.isUndefined = function (obj) {
return obj === void 0;
};

第二种用途中,作为 html 中 href 的 JavaScript 伪协议值。这个值将阻止页面进行跳转。

1
<a href="javascript:void(0);">空链接</a>

Google通过Code School提供免费Web开发教程

发表于 2013-03-28 | | 阅读次数

JavaScript数组队尾添加元素的方法

发表于 2013-02-23 | 分类于 技术水波文 | | 阅读次数

往数组末尾添加元素的方式通常是采用数组的 push 方法,如:

1
2
3
var array = [1, 2, 3];
array.push(4);
console.log(array); // [1, 2, 3, 4]

在 underscore.js 中采用的另一种小技巧:

1
2
var array = [1, 2, 3];
array[array.length] = 4;

JavaScript 数组是从 0 开始索引,所以数组中最后一个元素的索引值就是 数组的长度 - 1。往数组末尾添加元素可对 数组最后一个元素 + 1 索引的位置进行赋值,也就是对 array[array.length] 进行赋值。

需要注意的是,在循环往数组中添加元素的时候应采用 array.length,而非将 array.length 存储在局部变量中:

1
2
3
4
5
6
7
8
9
10
// 循环添加元素时,将array.length存储在变量(len)中
// 并通过array(len)添加元素时将出现非预期的行为
var array = [1, 2, 3];
var l = array.length;
for (var i = 0; i <= l; i++) {
array[l] = i;
}
console.log(array); // [1, 2, 3, 3]

我们的预期结果是 [1, 2, 3, 0, 1, 2, 3]:

1
2
3
4
5
6
7
8
var array = [1, 2, 3];
var l = array.length;
for (var i = 0; i <= l; i++) {
array[array.length] = i;
}
console.log(array); // [1, 2, 3, 0, 1, 2, 3]
1…212223…37
IIssNan

IIssNan

胡编一通,乱写一气

183 日志
6 分类
111 标签
RSS
GitHub Twitter 豆瓣 知乎
© 2011 - 2017 IIssNan
由 Hexo 强力驱动
主题 - NexT.Pisces
0%