Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .stfolder/syncthing-folder-ed52c7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This directory is a Syncthing folder marker.
# Do not delete.

folderID: zh.javascript.info
created: 2024-09-17T00:54:09+08:00
1 change: 1 addition & 0 deletions .stignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.git
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Chrome",
"request": "launch",
"type": "chrome",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}"
}
]
}
27 changes: 27 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello ${file}"
},
{
"label": "replace ALERT",
"type": "shell",
"command": "d:\\git\\usr\\bin\\sed -i 's/alert/console.log/g' ${file}"
},
{
"label": "gvim",
"type": "shell",
"command": "D:/Vim/vim91/gvim.exe ${file}"
}
{
"label": "git add",
"type": "shell",
"command": "D:/Git/mingw64/libexec/git-core/git.exe add ${file}"
}
]
}
5 changes: 5 additions & 0 deletions 1-js/05-data-types/03-string/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

let str = 'Hi';

str[0] = 'h'; // error
console.log( str[0] ); // 不成功
2 changes: 1 addition & 1 deletion 1-js/05-data-types/03-string/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ for (let char of "Hello") {
let str = 'Hi';

str[0] = 'h'; // error
alert( str[0] ); // 无法运行
alert( str[0] ); // 没成功
```

通常的解决方法是创建一个新的字符串,并将其分配给 `str` 而不是以前的字符串。
Expand Down
Empty file modified 1-js/05-data-types/04-array/article.md
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions 1-js/05-data-types/04-array/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

console.log(1);
console.log(2);
22 changes: 22 additions & 0 deletions 1-js/05-data-types/05-array-methods/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

let army = {
minAge: 18,
maxAge: 27,
canJoin(user) {
return user.age >= this.minAge && user.age < this.maxAge;
}
};

let users = [
{age: 16},
{age: 20},
{age: 23},
{age: 30}
];

// 找到 army.canJoin 返回 true 的 user
let soldiers = users.filter(army.canJoin, army);

console.log(soldiers.length); // 2
console.log(soldiers[0].age); // 20
console.log(soldiers[1].age); // 23
Empty file modified 1-js/05-data-types/05-array-methods/article.md
100644 → 100755
Empty file.
Empty file modified 1-js/05-data-types/06-iterable/article.md
100644 → 100755
Empty file.
Empty file modified 1-js/05-data-types/07-map-set/article.md
100644 → 100755
Empty file.
14 changes: 14 additions & 0 deletions 1-js/05-data-types/08-weakmap-weakset/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

let john = { name: "John" };

let weakMap = new WeakMap();
weakMap.set(john, "...");

john = null; // 覆盖引用

console.log(weakMap);
// WeakMap { <items unknown> }

// console.log(weakMap.get())

// john 被从内存中删除了!
Empty file modified 1-js/05-data-types/08-weakmap-weakset/article.md
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions 1-js/05-data-types/12-json/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file modified 1-js/05-data-types/12-json/article.md
100644 → 100755
Empty file.
Empty file modified 1-js/06-advanced-functions/04-var/article.md
100644 → 100755
Empty file.
20 changes: 20 additions & 0 deletions 1-js/06-advanced-functions/06-function-object/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

let sayHi = function() {
console.log("Hi");
};

console.log(sayHi);
console.log(sayHi.name); // sayHi(有名字!)

let aaa = sayHi;
console.log(aaa); // still sayHi, sure.
aaa();

let qqq = function(){
console.log("q");
};
console.log(qqq.name);

let ann = () => { console.log("a"); };
console.log(ann.name);
ann();
Empty file modified 1-js/06-advanced-functions/06-function-object/article.md
100644 → 100755
Empty file.
13 changes: 13 additions & 0 deletions 1-js/06-advanced-functions/06-function-object/func-obj1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

function sayHi() {
console.log("Hi");

// 计算调用次数
sayHi.counter++;
}
sayHi.counter = 0; // 初始值

sayHi(); // Hi
sayHi(); // Hi

console.log( `Called ${sayHi.counter} times` ); // Called 2 times
17 changes: 17 additions & 0 deletions 1-js/06-advanced-functions/06-function-object/func-obj2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

function makeCounter() {
// 不需要这个了
// let count = 0

function counter() {
return counter.count++;
};

counter.count = 0;

return counter;
}

let ct = makeCounter();
console.log( ct() ); // 0
console.log( ct() ); // 1
Empty file modified 1-js/06-advanced-functions/08-settimeout-setinterval/article.md
100644 → 100755
Empty file.
10 changes: 10 additions & 0 deletions 1-js/06-advanced-functions/08-settimeout-setinterval/timeout1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

let start = Date.now();
let times = [];

setTimeout(function run() {
times.push(Date.now() - start); // 保存前一个调用的延时

if (start + 100 < Date.now()) console.log(times); // 100 毫秒之后,显示延时信息
else setTimeout(run); // 否则重新调度
});
Empty file modified 1-js/06-advanced-functions/09-call-apply-decorators/article.md
100644 → 100755
Empty file.
29 changes: 29 additions & 0 deletions 1-js/06-advanced-functions/09-call-apply-decorators/deco0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

function slow(x) {
// 这里可能会有重负载的 CPU 密集型工作
console.log(`Called with ${x}`);
return x;
}

function cachingDecorator(func) {
let cache = new Map();

return function(x) {
if (cache.has(x)) { // 如果缓存中有对应的结果
return cache.get(x); // 从缓存中读取结果
}

let result = func(x); // 否则就调用 func

cache.set(x, result); // 然后将结果缓存(记住)下来
return result;
};
}

slow = cachingDecorator(slow);

console.log( slow(1) ); // slow(1) 被缓存下来了,并返回结果
console.log( "Again: " + slow(1) ); // 返回缓存中的 slow(1) 的结果

console.log( slow(2) ); // slow(2) 被缓存下来了,并返回结果
console.log( "Again: " + slow(2) ); // 返回缓存中的 slow(2) 的结果
34 changes: 34 additions & 0 deletions 1-js/06-advanced-functions/09-call-apply-decorators/deco1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

let worker = {

someMethod() {
return 1;
},

slow(x) {
// 可怕的 CPU 过载任务
console.log("Called with " + x);
return x * this.someMethod(); // (*)
}

};

// 和之前例子中的代码相同
function cachingDecorator(func) {
let cache = new Map();
return function(x) {
if (cache.has(x)) {
return cache.get(x);
}

let result = func(x); // (**)
cache.set(x, result);
return result;
};
}

console.log( worker.slow(1) ); // 原始方法有效

worker.slow = cachingDecorator(worker.slow); // 现在对其进行缓存

console.log( worker.slow(2) ); // 蛤!Error: Cannot read property 'someMethod' of undefined
Empty file modified 1-js/06-advanced-functions/10-bind/article.md
100644 → 100755
Empty file.
11 changes: 11 additions & 0 deletions 1-js/06-advanced-functions/10-bind/bind1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

let user = {
firstName: "John"
};

function f1() {
console.log(this.firstName);
}

let funcUser = f1.bind(user);
funcUser(); // John
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

console.log(Math.PI);

let descriptor = Object.getOwnPropertyDescriptor(Math, 'PI');

console.log( JSON.stringify(descriptor, null, 2 ) );
/*
{
"value": 3.141592653589793,
"writable": false,
"enumerable": false,
"configurable": false
}
*/

Math.PI = 3; // Error,因为其 writable: false

console.log(Math.PI);
// 3.141592653589793

// Error,因为 configurable: false
// Object.defineProperty(Math, "PI", { writable: true });
// TypeError: Cannot redefine property: PI




let user = {
name: "John"
};

Object.defineProperty(user, "name", {
configurable: false
});

user.name = "Pete"; // 正常工作
console.log(user);
// { name: 'Pete' }

delete user.name; // Error
console.log(user);
// { name: 'Pete' }
16 changes: 16 additions & 0 deletions 1-js/07-object-properties/01-property-descriptors/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

let user = {
name: "John"
};

let descriptor = Object.getOwnPropertyDescriptor(user, 'name');

console.log( JSON.stringify(descriptor, null, 2 ) );
/* 属性描述符:
{
"value": "John",
"writable": true,
"enumerable": true,
"configurable": true
}
*/
63 changes: 63 additions & 0 deletions 1-js/07-object-properties/01-property-descriptors/prop-desc2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

let user = {};

Object.defineProperty(user, "name", {
value: "John"
});

let descriptor = Object.getOwnPropertyDescriptor(user, 'name');

console.log( JSON.stringify(descriptor, null, 2 ) );
/*
{
"value": "John",
"writable": false,
"enumerable": false,
"configurable": false
}
*/

console.log(user);
// {}


Object.defineProperty(user, "addr", {
value: "221B",
enumerable: true,
});
console.log(user);
// { addr: '221B' }

user.name = "rose";
// no effect
console.log(user);
// { addr: '221B' }

user.addr="13F"
// no effect
console.log(user);
// { addr: '221B' }

Object.defineProperty(user, "nick", {
value: "nnn",
enumerable: true,
// configurable: true,
writable: true,
});
console.log(user);
// { addr: '221B', nick: 'nnn' }

user.nick="mmm";
console.log(user);
// { addr: '221B', nick: 'mmm' }

Object.defineProperty(user, "ROAttr", {
value : "init val",
enumerable: true,
writable: false,
});
console.log(user);
// { addr: '221B', nick: 'mmm', ROAttr: 'init val' }
user.ROAttr = "failing change";
console.log(user);
// { addr: '221B', nick: 'mmm', ROAttr: 'init val' }
Loading