Skip to content

Instantly share code, notes, and snippets.

View GeekaholicLin's full-sized avatar
💭
I may be slow to respond.

void GeekaholicLin

💭
I may be slow to respond.
View GitHub Profile
@GeekaholicLin
GeekaholicLin / flattern.md
Created September 4, 2016 08:59
数组扁平化

两级数组嵌套扁平化

Array.prototype.concat.apply([],array);

function flattern(arr){
  var myArray = Array.prototype.concat.apply([],arr);
console.log(myArray);
}
flattern([[1],[2,3],[4,5,6]]);//logs [1, 2, 3, 4, 5, 6]
@GeekaholicLin
GeekaholicLin / 名称解析顺序.md
Created September 3, 2016 13:43
名称解析顺序

当访问函数内的 foo 变量时,JavaScript 会按照下面顺序查找:

当前作用域内是否有 var foo 的定义。
函数形式参数是否有使用 foo 名称的。
函数自身是否叫做 foo。
回溯到上一级作用域,然后从 #1 重新开始。

参考链接

@GeekaholicLin
GeekaholicLin / call_and_apply.md
Last active December 6, 2016 13:13
the trick of using call and apply together[当call和apply一起使用]

在看Javascript 秘密花园的时候,发现一段晦涩难懂的代码片段,由于 中文版的没有讲到这个技巧的妙处,所以硬着头皮,看看英文版

Another trick is to use both call and apply together to turn methods - functions that use the value of this as well as their arguments - into normal functions which only use their arguments.

(大概的意思是:可以一起使用callapply将使用this和参数的方法函数转化为使用相同参数的普通函数), 这不是在普通函数中利用callapply,绑定传入的上下文吗?嗯...挺类似的,但是它两个一起使用,因为上下文是一个函数,需要再利用一次。

全部代码如下

@GeekaholicLin
GeekaholicLin / this_foo.md
Last active September 3, 2016 08:28
javascript 易错点

this 易错点

Foo.method = function() {
    function test() {
        // this 将会被设置为全局对象(译者注:浏览器环境中也就是 window 对象)
    }
    test();
}
@GeekaholicLin
GeekaholicLin / hasOwnProperty.md
Created September 3, 2016 07:54
当hasOwnProperty作为对象属性的时候,如何使用hasOwnProperty方法
@GeekaholicLin
GeekaholicLin / find_character.md
Created September 2, 2016 03:32
find if the string contains the characters of substring or not[判断字符串是否包含字符,与顺序无关]
function mutation(arr) {
  //build a regexp
  var regStr = "";
  arr[1].split("").forEach(function(val){
      regStr +="(?=.*?"+val.toLowerCase()+")";
    //just like this "/(?=.*?a)(?=.*?b)(?=.*?c)/"
  });
  var regObj = new RegExp(regStr,"i");
 return regObj.test(arr[0]);

从写程序的人来说,你描述的后半截是成立的,比如字符型、bool型、长度等信息表示元数据,而姓名(元数:字符型,6字节)、性别(元数:整型、1字节)是业务数据,这是从开发程序的角度。

但是前半截,也就是博主所说的意思,是从普(业)通(务)人的角度来看,如姓名、年龄、身高是元数据,而张三、男、170cm则是业务数据。

http://www.ruanyifeng.com/blog/2007/03/metadata.html#comment-353332

@GeekaholicLin
GeekaholicLin / falsy_values.md
Created August 30, 2016 09:19
filter the falsy values in the array[过滤假值]
function bouncer(arr) {
  arr = arr.filter(function(val){
    //Boolean在这为转型函数而不是基本包装类
    //当val为假值即false、null、0、""、undefined 和 NaN时,Boolean(val)为false
    return Boolean(val);
  });
 return arr;
}