JavaScript 学习笔记
暂时没空从头开始搞…用到哪里先记录一下好了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
console.log(o.a); // 1
// a是o的自身属性吗?是的,该属性的值为1
1console.log(o.b); // 2
2// b是o的自身属性吗?是的,该属性的值为2
3// 原型上也有一个'b'属性,但是它不会被访问到.这种情况称为"属性遮蔽 (property shadowing)"
1console.log(o.c); // 4
2// c是o的自身属性吗?不是,那看看原型上有没有
3// c是o.[[Prototype]]的属性吗?是的,该属性的值为4
1console.log(o.d); // undefined
2// d是o的自身属性吗?不是,那看看原型上有没有
3// d是o.[[Prototype]]的属性吗?不是,那看看它的原型上有没有
4// o.[[Prototype]].[[Prototype]] 为 null,停止搜索
5// 没有d属性,返回undefined
1 hasOwnProperty方法,用来检查对象是否有自己定义的属性,而不是从原型链上继承的属性。
2该方法不需要遍历原型链。
3function Graph() {
4 this.vertices = [];
5 this.edges = [];
6}
1Graph.prototype = {
2 addVertex: function(v){
3 this.vertices.push(v);
4 }
5};
console.log(g.hasOwnProperty('vertices'));
// true
console.log(g.hasOwnProperty('nope'));
// false
console.log(g.hasOwnProperty('addVertex'));
// false
console.log(g.__proto__.hasOwnProperty('addVertex'));
// true
1var g = new Graph();
2// g是生成的对象,他的自身属性有'vertices'和'edges'.
3// 在g被实例化时,g.[[Prototype]]指向了Graph.prototype.
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
1// 1 + 2 + 3 + 4
2console.log(array1.reduce(reducer));
3// expected output: 10
1// 5 + 1 + 2 + 3 + 4
2console.log(array1.reduce(reducer, 5));
3// expected output: 15
1const object1 = {
2 a: 1,
3 b: 2,
4 c: 3,
5 a: 4
6};
const object2 = Object.assign({a: 14, d: 5,h:12}, object1);
1console.log(object2.a, object2.d,object2.h);
2// expected output: 1 5 12
3// Properties in the target object will be overwritten by properties in the sources if they have the same key.
4//Later sources' properties will similarly overwrite earlier ones.
1function multiply(multiplier, ...theArgs) {
2 return theArgs.map(function (element) {
3 return multiplier * element;
4 });
5}
var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]
function parentFunc() {
var a = 1;
1 function nestedFunc() {
2 var b = 4; // parentFunc can't use this
3 return a + b;
4 }
5 return nestedFunc(); // 5
6}
1(参数1, 参数2, …, 参数N) => { 函数声明 }
2(参数1, 参数2, …, 参数N) => 表达式(单一)
3//相当于:(参数1, 参数2, …, 参数N) =>{ return 表达式; }
1// 当只有一个参数时,圆括号是可选的:
2(单一参数) => {函数声明}
3单一参数 => {函数声明}
// 没有参数的函数应该写成一对圆括号。
() => {函数声明}
1var materials = [
2 'Hydrogen',
3 'Helium',
4 'Lithium',
5 'Beryllium'
6];
1materials.map(function(material) {
2 return material.length;
3}); // [8, 6, 7, 9]
1materials.map((material) => {
2 return material.length;
3}); // [8, 6, 7, 9]
materials.map(material => material.length); // [8, 6, 7, 9]
class ChildClass extends ParentClass { ... }