Skip to content

Instantly share code, notes, and snippets.

@hjzheng
hjzheng / login.exp
Created October 23, 2015 02:41
用于iTerm2的自动登录脚本
#!/usr/bin/expect
set timeout 30
spawn ssh [lindex $argv 0]@[lindex $argv 1]
expect {
"(yes/no)?"
{send "yes\n";exp_continue}
"password:"
{send "[lindex $argv 2]\n"}
}
@hjzheng
hjzheng / telnet.exp
Last active October 27, 2015 02:59
用于iTerm2的自动telnet脚本
#!/usr/bin/expect #Where the script should be run from.
#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
@hjzheng
hjzheng / oclazyload-demo.md
Last active February 3, 2016 06:48
oclazyload结合ui router的使用方式

index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>路由分别加载controller测试</title>
		<script src="bower_components/angular/angular.js" type="text/javascript" charset="utf-8"></script>
		<script src="bower_components/angular-ui-router/release/angular-ui-router.js" type="text/javascript" charset="utf-8"></script>
		<script src="bower_components/oclazyload/dist/ocLazyLoad.js" type="text/javascript" charset="utf-8"></script>

理解作用域链

JavaScript权威指南 P59 中关于作用域链解释的不是很清楚。

JavaScript代码在执行的过程中,需要依赖其执行上下文(Execution Context), 当JavaScript代码被浏览器载入后,默认最先进入一个全局执行上下文,当在全局上下文中调用执行一个函数时,程序流就进入该被调用函数内,此时引擎就会为该函数创建一个新的执行上下文,并且将其压入到执行上下文堆栈的顶部。浏览器总是执行当前在堆栈顶部的上下文,一旦执行完毕,该上下文就会从堆栈顶部被弹出,然后,进入其下的上下文执行代码。这样,堆栈中的上下文就会被依次执行并且弹出堆栈,直到回到全局的上下文。

关于执行上下文(Execution Context) 可以将执行上下文,理解成为一个Object。它主要包含三个属性 变量对象,this 指向 和作用域链。

变量对象包括 函数的形参,函数声明和变量声明

对象

对象的属性特性 (property attribute)

  • 可写 (writable attribute), 表明是否可以设置该属性的值
  • 可枚举 (enumerable attribute), 表明是否可以通过for/in循环返回该属性
  • 可配置 (configurable attribute),表明是否可以删除或修改该属性

对象特性 (object attribute)

  • 对象的原型 (prototype) 指向另外一个对象,本对象的属性继承自它的原型对象
  • 对象的类 (class)是一个标识对象类型的字符串
  • 对象的扩展标记 (extensible flag)指明了是否可以向该对象添加新属性

心态

  • 巩固基础
  • 空杯和对技术点敬畏的心态
  • 洗白自己

查录补缺

重读犀牛书

  • 可选分号 (保持良好的编码习惯,加分号)

数组

创建数组的方式

  • 字面量方式
  • Array类方式

注意一下数组创建产生的结果:

var count = [1,,3]; //数组有3个元素,中间的元素值是undefined 这个书上描述是错误的,中间元素,是一个空的位置
var undefs = [,,]; //数组有2个元素,都是undefined 原因:数组字面量的语法允许有可选的结尾逗号 书上描述是错误的 是两个空位置
var a = new Array(3); 

函数

函数的四种调用方式(关于this指向问题)

  • 方法调用模式
  • 函数调用模式
  • 构造器调用模式
  • apply/call调用模式

具体细节参见我之前在segmentfault上的回答

@hjzheng
hjzheng / advice.js
Created March 15, 2016 03:13 — forked from angus-c/advice.js
an advice functional mixin
//usage
withAdvice.call(targetObject);
//mixin augments target object with around, before and after methods
//method is the base method, advice is the augmenting function
withAdvice: function() {
['before', 'after', 'around'].forEach(function(m) {
this[m] = function(method, advice) {
if (typeof this[method] == 'function') {
return this[method] = fn[m](this[method], advice);