Skip to main content
  1. Posts/

JavaScript 学习笔记

·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

暂时没空从头开始搞…用到哪里先记录一下好了orz

我觉得不行,还是要先大致了解一下。

参考资料:

A re-introduction to JavaScript (JS tutorial)

继承与原型链

  1// 让我们假设我们有一个对象 o, 其有自己的属性 a 和 b:
  2// {a: 1, b: 2}
  3// o 的 [[Prototype]] 有属性 b 和 c:
  4// {b: 3, c: 4}
  5// 最后, o.[[Prototype]].[[Prototype]] 是 null.
  6// 这就是原型链的末尾,即 null,
  7// 根据定义,null 没有[[Prototype]].
  8// 综上,整个原型链如下: 
  9// {a:1, b:2} ---> {b:3, c:4} ---> null
 10
 11console.log(o.a); // 1
 12// a是o的自身属性吗?是的,该属性的值为1
 13
 14console.log(o.b); // 2
 15// b是o的自身属性吗?是的,该属性的值为2
 16// 原型上也有一个'b'属性,但是它不会被访问到.这种情况称为"属性遮蔽 (property shadowing)"
 17
 18console.log(o.c); // 4
 19// c是o的自身属性吗?不是,那看看原型上有没有
 20// c是o.[[Prototype]]的属性吗?是的,该属性的值为4
 21
 22console.log(o.d); // undefined
 23// d是o的自身属性吗?不是,那看看原型上有没有
 24// d是o.[[Prototype]]的属性吗?不是,那看看它的原型上有没有
 25// o.[[Prototype]].[[Prototype]] 为 null,停止搜索
 26// 没有d属性,返回undefined
 27
 28
 29
 30
 31 hasOwnProperty方法,用来检查对象是否有自己定义的属性而不是从原型链上继承的属性
 32该方法不需要遍历原型链
 33function Graph() {
 34  this.vertices = [];
 35  this.edges = [];
 36}
 37
 38Graph.prototype = {
 39  addVertex: function(v){
 40    this.vertices.push(v);
 41  }
 42};
 43
 44console.log(g.hasOwnProperty('vertices'));
 45// true
 46
 47console.log(g.hasOwnProperty('nope'));
 48// false
 49
 50console.log(g.hasOwnProperty('addVertex'));
 51// false
 52
 53console.log(g.__proto__.hasOwnProperty('addVertex'));
 54// true
 55
 56var g = new Graph();
 57// g是生成的对象,他的自身属性有'vertices'和'edges'.
 58// 在g被实例化时,g.[[Prototype]]指向了Graph.prototype.
 59
 60
 61
 62
 63
 64const array1 = [1, 2, 3, 4];
 65const reducer = (accumulator, currentValue) => accumulator + currentValue;
 66
 67// 1 + 2 + 3 + 4
 68console.log(array1.reduce(reducer));
 69// expected output: 10
 70
 71// 5 + 1 + 2 + 3 + 4
 72console.log(array1.reduce(reducer, 5));
 73// expected output: 15
 74
 75
 76
 77
 78const object1 = {
 79  a: 1,
 80  b: 2,
 81  c: 3,
 82  a: 4
 83};
 84
 85const object2 = Object.assign({a: 14, d: 5,h:12}, object1);
 86
 87console.log(object2.a, object2.d,object2.h);
 88// expected output: 1 5 12
 89// Properties in the target object will be overwritten by properties in the sources if they have the same key. 
 90//Later sources' properties will similarly overwrite earlier ones.
 91
 92
 93
 94
 95
 96function multiply(multiplier, ...theArgs) {
 97  return theArgs.map(function (element) {
 98    return multiplier * element;
 99  });
100}
101
102var arr = multiply(2, 1, 2, 3); 
103console.log(arr);  // [2, 4, 6]
104
105
106
107
108function parentFunc() {
109  var a = 1;
110
111  function nestedFunc() {
112    var b = 4; // parentFunc can't use this
113    return a + b; 
114  }
115  return nestedFunc(); // 5
116}
117
118
119
120
121(参数1, 参数2, , 参数N) => { 函数声明 }
122(参数1, 参数2, , 参数N) => 表达式单一
123//相当于:(参数1, 参数2, …, 参数N) =>{ return 表达式; }
124
125// 当只有一个参数时,圆括号是可选的:
126(单一参数) => {函数声明}
127单一参数 => {函数声明}
128
129// 没有参数的函数应该写成一对圆括号。
130() => {函数声明}
131
132var materials = [
133  'Hydrogen',
134  'Helium',
135  'Lithium',
136  'Beryllium'
137];
138
139materials.map(function(material) { 
140  return material.length; 
141}); // [8, 6, 7, 9]
142
143materials.map((material) => {
144  return material.length;
145}); // [8, 6, 7, 9]
146
147materials.map(material => material.length); // [8, 6, 7, 9]
148
149
150
151
152class ChildClass extends ParentClass { ... }

Related

[C++11] promise && future leanrning notes

用人话就是,主线程传给附属线程一个promise Object,然后主线程想要获取附属线程set给promise Object的值(也就是该线程返回的某个结果),需要通过主线程中的promise object 得到对应的future object(每个promise 对应一个 future),然后调用future 的get方法。如果附属线程没有执行作为参数传入的promise的set方法去返回结果,那么程序就会block住。

把二进制文件按字节读到vector中

·1 min
1 std::vector<unsigned char> readFromFile1(const char* filePath) { 2 FILE* file = fopen(filePath, "rb"); 3 std::vector<unsigned char> result; 4 if (file == nullptr) { 5 return result; 6 } 7 8 // 获取文件大小,尽量一次读完 9 size_t fileSize = getFileSize(file); 10 if (fileSize != 0) { 11 result.resize(fileSize); 12 size_t n = fread(&result[0], 1, fileSize, file); 13 assert(n <= fileSize); 14 if (n != fileSize) { 15 result.resize(n); 16 } 17 } 18 19 // 在读取过程当中,有可能文件大小有变化,再尝试读取 20 const size_t read_len = 1024; 21 char buf[read_len]; 22 for (;;) { 23 size_t n = fread(buf, 1, read_len, file); 24 result.insert(result.end(), buf, buf + n); 25 if (n < read_len) { 26 break; 27 } 28 } 29 fclose(file); 30 return result; 31 } 另外一种C++风格,但是性能较差的方法: