LinkXSystem / learn-guide

有趣的学习笔记 (*^_^*)
https://linkxsystem.github.io/
3 stars 1 forks source link

Javascript 小技巧 (。・∀・)ノ゙ #44

Open LinkXSystem opened 3 years ago

LinkXSystem commented 3 years ago

_new 函数

function _new() {
    const obj = new Object();

    // 提取 constructor 函数
    Constructor = [].shift.call(arguments);
    obj.__proto__ = Constructor.prototype;

    const ret = Constructor.apply(obj, arguments);

    return typeof ret === 'object' ? ret || obj : obj;
}

参考文章:JavaScript深入之new的模拟实现

LinkXSystem commented 3 years ago

网页从服务器拿到数据,需要保存下来,但是不想存在 Cookie 和 LocalStorage,怎么办?这篇文章的方法就是动态生成一张 Canvas 的 PNG 图片,将数据存在里面。

Hijacking HTML canvas and PNG images to store arbitrary text data

本质实现这个功能是,二进制的数据的转换。 在 Canvas 中,一个单元是 [r, g, b, a] 的元组。均支持 0- 255 的范围,那实现这样的一个功能便非常明显。