Skip to content

Commit 64ba8a5

Browse files
committed
更新js-lab
1 parent 8b454a8 commit 64ba8a5

30 files changed

+12903
-12748
lines changed

.devcontainer/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM node:23
2+
3+
WORKDIR /home/
4+
5+
COPY . .
6+
7+
RUN bash ./setup.sh
8+
9+
10+

.devcontainer/devcontainer.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Codespaces JS/TS",
3+
"extensions": [
4+
"github.vscode-github-actions"
5+
],
6+
"dockerFile": "Dockerfile",
7+
"settings": {
8+
"editor.formatOnSave": true,
9+
"files.exclude": {
10+
"**/CODE_OF_CONDUCT.md": true,
11+
"**/LICENSE": true
12+
}
13+
}
14+
// "runArgs": ["-e","HTTP_PROXY=http://192.168.21.233:7890","-e","HTTPS_PROXY=http://192.168.21.233:7890"]
15+
}

.devcontainer/setup.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm install -g typescript

LICENSE

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2023 Li GuangQiao
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2023 Li GuangQiao
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

array.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
// const people = [
2-
// {
3-
// name: "Matt",
4-
// age: 27
5-
// },
6-
// {
7-
// name: "Nicholas",
8-
// age: 29
9-
// }
10-
// ];
11-
// console.log(people.find((element, index, array) => element.age < 28));
12-
// // {name: "Matt", age: 27}
13-
// console.log(people.findIndex((element, index, array) => element.age < 28));
14-
// // 0
15-
16-
// console.log(people.find((element, index, array) => element.age == 28));
17-
// // undefined
18-
// console.log(people.findIndex((element, index, array) => element.age == 28));
19-
// // -1
20-
let arr = new Array(10);
21-
console.log("arr:",arr);
22-
arr = new Array({
23-
id:1
24-
},{
25-
my_id:2
26-
})
1+
// const people = [
2+
// {
3+
// name: "Matt",
4+
// age: 27
5+
// },
6+
// {
7+
// name: "Nicholas",
8+
// age: 29
9+
// }
10+
// ];
11+
// console.log(people.find((element, index, array) => element.age < 28));
12+
// // {name: "Matt", age: 27}
13+
// console.log(people.findIndex((element, index, array) => element.age < 28));
14+
// // 0
15+
16+
// console.log(people.find((element, index, array) => element.age == 28));
17+
// // undefined
18+
// console.log(people.findIndex((element, index, array) => element.age == 28));
19+
// // -1
20+
let arr = new Array(10);
21+
console.log("arr:",arr);
22+
arr = new Array({
23+
id:1
24+
},{
25+
my_id:2
26+
})
2727
console.log("arr:",arr);

arraybuffer.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// 创建长度为 8 的 int16 数组
2-
const container = new Int16Array(8);
3-
// 把定型数组复制为前 4 个值
4-
// 偏移量默认为索引 0
5-
container.set(Int8Array.of(1, 2, 3, 4));
6-
console.log(container); // [1,2,3,4,0,0,0,0]
7-
// 把普通数组复制为后 4 个值
8-
// 偏移量 4 表示从索引 4 开始插入
9-
container.set([5,6,7,8], 4);
10-
console.log(container); // [1,2,3,4,5,6,7,8]
11-
// 溢出会抛出错误
12-
container.set([5,6,7,8], 7);
1+
// 创建长度为 8 的 int16 数组
2+
const container = new Int16Array(8);
3+
// 把定型数组复制为前 4 个值
4+
// 偏移量默认为索引 0
5+
container.set(Int8Array.of(1, 2, 3, 4));
6+
console.log(container); // [1,2,3,4,0,0,0,0]
7+
// 把普通数组复制为后 4 个值
8+
// 偏移量 4 表示从索引 4 开始插入
9+
container.set([5,6,7,8], 4);
10+
console.log(container); // [1,2,3,4,5,6,7,8]
11+
// 溢出会抛出错误
12+
container.set([5,6,7,8], 7);
1313
// RangeError

class_test/base.js

+57-57
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
const private_map = new WeakMap();
2-
const Observer = require("./observer");
3-
class Base {
4-
initInternalObj(type,configs){
5-
let me = this;
6-
const interalObj = Object.assign({
7-
"type":type
8-
},configs);
9-
const proxy = new Proxy(interalObj,{
10-
get(obj_self,key,self){
11-
me.getObserver().fireEvent("read","reading event testing");
12-
console.log(`Getting ${key}'s value`);
13-
return Reflect.get(obj_self,key,self);
14-
},
15-
set(obj_self,key,newValue,self){
16-
me.getObserver().fireEvent("write","writing event testing");
17-
console.log(`Setting ${key}'s value to ${newValue}`);
18-
return Reflect.set(obj_self,key,newValue,self);
19-
}
20-
});
21-
return proxy;
22-
}
23-
on(eventName,fn){
24-
this.getObserver().addListener(eventName,fn);
25-
}
26-
constructor(type,configs) {
27-
this.idProperty = {
28-
id:Symbol("id")
29-
};
30-
this.initObserver();
31-
this.init(this.initInternalObj(type,configs));
32-
}
33-
initObserver(){
34-
let observer ={
35-
"observer":Symbol("observer")
36-
};
37-
private_map.set(observer,new Observer(this));
38-
this.getObserver = function(){
39-
return private_map.get(observer);
40-
};
41-
}
42-
initProperty(configs){
43-
private_map.set(this.idProperty,configs);
44-
}
45-
set(key,val){
46-
let property = private_map.get(this.idProperty);
47-
property[key] = val;
48-
}
49-
get(key){
50-
return private_map.get(this.idProperty)[key];
51-
}
52-
init(configs){
53-
this.initProperty(configs);
54-
}
55-
56-
}
57-
module.exports = Base;
1+
const private_map = new WeakMap();
2+
const Observer = require("./observer");
3+
class Base {
4+
initInternalObj(type,configs){
5+
let me = this;
6+
const interalObj = Object.assign({
7+
"type":type
8+
},configs);
9+
const proxy = new Proxy(interalObj,{
10+
get(obj_self,key,self){
11+
me.getObserver().fireEvent("read","reading event testing");
12+
console.log(`Getting ${key}'s value`);
13+
return Reflect.get(obj_self,key,self);
14+
},
15+
set(obj_self,key,newValue,self){
16+
me.getObserver().fireEvent("write","writing event testing");
17+
console.log(`Setting ${key}'s value to ${newValue}`);
18+
return Reflect.set(obj_self,key,newValue,self);
19+
}
20+
});
21+
return proxy;
22+
}
23+
on(eventName,fn){
24+
this.getObserver().addListener(eventName,fn);
25+
}
26+
constructor(type,configs) {
27+
this.idProperty = {
28+
id:Symbol("id")
29+
};
30+
this.initObserver();
31+
this.init(this.initInternalObj(type,configs));
32+
}
33+
initObserver(){
34+
let observer ={
35+
"observer":Symbol("observer")
36+
};
37+
private_map.set(observer,new Observer(this));
38+
this.getObserver = function(){
39+
return private_map.get(observer);
40+
};
41+
}
42+
initProperty(configs){
43+
private_map.set(this.idProperty,configs);
44+
}
45+
set(key,val){
46+
let property = private_map.get(this.idProperty);
47+
property[key] = val;
48+
}
49+
get(key){
50+
return private_map.get(this.idProperty)[key];
51+
}
52+
init(configs){
53+
this.initProperty(configs);
54+
}
55+
56+
}
57+
module.exports = Base;

class_test/observer.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
class Observer{
2-
events = new Map();
3-
target = null;
4-
constructor(belong){
5-
this.id = Symbol(belong);
6-
this.target = belong;
7-
}
8-
//事件添加函数
9-
addListener(eventName,fn){
10-
let same_events = this.events.get(eventName)?this.events.get(eventName):[],
11-
setFlag = same_events.length === 0;
12-
same_events.push(fn);
13-
setFlag&&this.events.set(eventName,same_events);
14-
}
15-
fireEvent(eventName,...args){
16-
let same_events = this.events.get(eventName)?this.events.get(eventName):[],
17-
argsArray = [eventName].concat(args);
18-
if(!this.target){
19-
throw new Error("Oberviser can not be using while property target is null.");
20-
}
21-
if(same_events.length !== 0){
22-
this.doFireEvent(same_events,argsArray);
23-
}
24-
}
25-
doFireEvent(events,args){
26-
let eventFns = events.values();
27-
for(let fn of eventFns){
28-
fn.apply(this.target,args);
29-
}
30-
}
31-
}
32-
module.exports = Observer;
1+
class Observer{
2+
events = new Map();
3+
target = null;
4+
constructor(belong){
5+
this.id = Symbol(belong);
6+
this.target = belong;
7+
}
8+
//事件添加函数
9+
addListener(eventName,fn){
10+
let same_events = this.events.get(eventName)?this.events.get(eventName):[],
11+
setFlag = same_events.length === 0;
12+
same_events.push(fn);
13+
setFlag&&this.events.set(eventName,same_events);
14+
}
15+
fireEvent(eventName,...args){
16+
let same_events = this.events.get(eventName)?this.events.get(eventName):[],
17+
argsArray = [eventName].concat(args);
18+
if(!this.target){
19+
throw new Error("Oberviser can not be using while property target is null.");
20+
}
21+
if(same_events.length !== 0){
22+
this.doFireEvent(same_events,argsArray);
23+
}
24+
}
25+
doFireEvent(events,args){
26+
let eventFns = events.values();
27+
for(let fn of eventFns){
28+
fn.apply(this.target,args);
29+
}
30+
}
31+
}
32+
module.exports = Observer;
3333
// console.log(new Oberviser().target);

class_test/test.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const Base = require('./base');
2-
let t = new Base("test",{
3-
test:"测试ing"
4-
});
5-
6-
t.on("read",function(arg1,arg2){
7-
console.log("测试读监听器:",arguments,arg1,arg2);
8-
});
9-
t.on("write",function(arg1,arg2){
10-
console.log("测试写监听器:",arguments,arg1,arg2);
11-
});
12-
console.log(t.get("test"));
13-
t.set("demo","heihei");
14-
console.log(t.get("demo"));
15-
// let d = function(){
16-
// console.log(this,arguments);
17-
// };
18-
// // d(1,Array.prototype.slice.call(["t","a"], 0));
19-
// d.apply({
20-
// "ceshi":"测试"
21-
// },[1,"2",'t'])
22-
1+
const Base = require('./base');
2+
let t = new Base("test",{
3+
test:"测试ing"
4+
});
5+
6+
t.on("read",function(arg1,arg2){
7+
console.log("测试读监听器:",arguments,arg1,arg2);
8+
});
9+
t.on("write",function(arg1,arg2){
10+
console.log("测试写监听器:",arguments,arg1,arg2);
11+
});
12+
console.log(t.get("test"));
13+
t.set("demo","heihei");
14+
console.log(t.get("demo"));
15+
// let d = function(){
16+
// console.log(this,arguments);
17+
// };
18+
// // d(1,Array.prototype.slice.call(["t","a"], 0));
19+
// d.apply({
20+
// "ceshi":"测试"
21+
// },[1,"2",'t'])
22+

0 commit comments

Comments
 (0)