Blockchain Techs
Ask yourselfWhat is Blockchain? It is a decentralised ledger that stores transaction data publicly. What is Ethereum? It is a blockchain that supports smart contracts. What is Smart Contract? It is the code deployed on Ethereum that runs autonomously and cannot be changed. What is Wallet? It is a way to store and control private keys (e.g. MetaMask) What is Transaction? It is a signed action recorded permanently on the blockchain. What is Testnet (e.g. Sepolia)? It is a testing version of Eth...
Closures
作用域链当我们访问一个变量的时候,JavaScript引擎首先会在当前作用域寻找这个变量。如果当前作用域没有这个变量,就回去上一层作用域寻找。如果上一层作用域找不到,就去上上层寻找。直到全局作用域`都找不到时,返回undefined。 1234567891011121314var windowVar = "windowVar";function outer() { var outerVar = "outerVar"; function inner() { var innerVar = "innerVar"; console.log(outerVar); // outerVar console.log(innerVar); // innerVar console.log(windowVar); // windowVar } inner();}outer(); 闭包定义:即使外部函数已经不存在...
Handwrite JavaScript
callSee this branch: https://github.com/leonleerl/handwrite-js/tree/call call 允许一个函数在另一个对象的上下文中执行,也就是说你可以借用一个函数,并临时将它的 this 指向任意对象。 call allows a function to be executed in the context of a different object, meaning you can borrow a function and temporarily point its this to any object you want. 123456789101112131415161718192021Function.prototype.myCall = function (context, ...args) { context = context || window; context.fn = this; const result = context.fn(...args); delete...
Garbage Collection in JavaScript
何为垃圾?test在栈内存,{ name: “test”}和[1, 2, 3, 4, 5]都在堆内存中,{ name: “test”}被覆盖掉了,成了堆内存中的垃圾。 12345let test = { name: "test"};test = [1, 2, 3, 4, 5];console.log(test); 引用计数法是一种早期浏览器使用的方法。
JavaScript This
this是在运行时绑定还是编译时绑定?this是用于访问当前方法所属的对象。 123456789101112131415function foo() { console.log(this);}var obj = { name: "obj", foo: foo}// 调用方式1: 直接调用foo();// 调用方式2: 通过对象调用obj.foo();// 调用方式3: 通过call或apply调用foo.call("hello world") 在浏览器中的运行结果: 123Window {window: Window, self: Window, document: document, name: '', location: Location, …}{name: 'obj', foo: ƒ}String {'hello world'} 通过这个例子,我们可以知...
Inheritance and Prototype
什么是构造函数任何函数都可以作为构造函数。当该函数通过 new 关键字调用的时候,我们就称之为构造函数。 12345678var Parent = function() {}// 定义一个函数,那它只是一个普通的函数,我们不能称它为构造函数var instance = new Parent();// 这时这个Parent就不是普通的函数了,而是构造函数,因为是用new调用了它// 创建了一个Parent构造函数的实例 instance prototype属性prototype是函数特有的属性。一句话概括就是:就让某一个构造函数实例化的所有对象可以找到公共的方法和属性。 1234567891011var Parent = function() {}Parent.prototype.name = "所有Parent的实例都可以读到我";let p1 = new Parent();let p2 = new Parent();console.log(p1.name); // 所有Parent的实例都可以读到我console...
ES6 key concepts
Async/Await在function前面加上Async关键字,会让这个函数的返回结果变成一个Promise对象。如代码所示。await关键字不能像async一样可以单独存在,只能放在async函数中,如test3所示。 1234567891011121314151617181920212223async function test() { return "Hello World";}async function test2() { return Promise.resolve("Hello World");}async function test3() { let result = await test2(); console.log(result);}let result = test();let result2 = test2();test3();console.log(result);console.log(result2);resu...
abi.encode and abi.encodePacked
abi.encode1234function stringEncode() public pure returns (bytes memory) { bytes memory someString = abi.encode("some string"); return someString;} The returned value: 12340x0000000000000000000000000000000000000000000000000000000000000020 // offset000000000000000000000000000000000000000000000000000000000000000b // length736f6d6520737472696e67000000000000000000000000000000000000000000 // data abi.encodePacked1234function stringEncodePacked() public pure returns (b...
Create Create2 Create3
In this article, we will discuss how to create contract address and deploy it onto a specific address on chain. CreateFirst, let’s explain how the address gets generated? When we use new Contract() (CREATE), 1contract address = hash(msg.sender + nonce) Which is essentially: 1keccak256( rlp.encode( sender, nonce ) )[12:] What is nonce? Creator meaning of nonce As EOA the transactions it has sent As Contract Factory the number of contracts it has generated The problem of CREATE: ...
What is Multisig On-chain Evidence
ConceptEasy way to explain: Multisig On-chain Evidence is an unchangeable, hashed evidence on chain, signed and confirmed by multiple users. Multisig: Multiple designated users confirm one specific thing. Evidence Anchoring: Store the hashed “fingerprint” data on the blockchain. Imagine a company is signing a contract, it should be signed by different stakeholders with their names on. Real implementations in web3.0 apps: DAO Governance (Core Use Case)DAOs rely on collective decision-making...
