Skip to content

Commit 783bd2c

Browse files
committed
新增String对象的实例方法 charAt( )、concat( )
1 parent 3717a1d commit 783bd2c

File tree

6 files changed

+244
-14
lines changed

6 files changed

+244
-14
lines changed

String/LString.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function WString() {
2+
this.value = []; // 初始化 wString对象 时,里面定义 一个空数组,将来用来装数据的容器
3+
this.length = 0; //因为是空数组,所以现在的长度是0
4+
/**
5+
* push()方法用于在数组的末端添加 数组的元素,并返回添加新元素后的数组长度。
6+
* 如果入参不合法,将会抛错;
7+
* 如果运行出错,将会返回空数组。
8+
* @param {...any} item1
9+
* @returns
10+
*/
11+
this.push = function (...item1) { // 当传入多个参数入来时 可用 ...形参名 表示
12+
13+
if (item1 == undefined || item1 == null || item1.length == 0) {
14+
throw new error('入参不合法');
15+
}
16+
try {
17+
for (let i = this.value.length, j = 0; j < item1.length; i++, j++) {
18+
this.value[i] = item1[j]
19+
}
20+
this.length += item1.length;
21+
return this.length;
22+
23+
} catch (error) {
24+
console.log(error)
25+
}
26+
};
27+
28+
/**
29+
* charAt() 方法返回指定位置的字符,参数是从0开始编号的位置。
30+
* @param {*} item
31+
* @returns
32+
*/
33+
this.chartAt = function (item) {
34+
if (item == undefined || item == null || item > this.value.length || item < 0) {
35+
return this.value[0];
36+
}
37+
try {
38+
for (let i = 0; i < this.value.length; i++) {
39+
if (i === item) {
40+
return this.value[i];
41+
}
42+
}
43+
} catch (error) {
44+
console.log(error)
45+
}
46+
47+
};
48+
/**
49+
* concat() 方法用于 连接 两个字符串,返回一个新字符串,不改变原字符串
50+
* 如果参数不是字符串,concat() 方法会将其转为字符串,然后再连接
51+
* @param {...any} concatStr
52+
* @returns
53+
*/
54+
this.concat = function (...concatStr) {
55+
if (concatStr == undefined) {
56+
concatStr = undefined
57+
}
58+
if (concatStr == null) {
59+
concatStr = null
60+
}
61+
try {
62+
let concatVal = this.value;
63+
for (let i = 0; i < concatStr.length; i++) {
64+
concatVal.push(concatStr[i]);
65+
};
66+
let val = concatVal.toString();
67+
let result = '';
68+
for (let j = 0; j < val.length; j++) {
69+
if (',' != val[j]) {
70+
result += val[j]
71+
};
72+
};
73+
return result;
74+
} catch (error) {
75+
console.log(error);
76+
}
77+
78+
};
79+
80+
}
81+
82+
let wString = new WString();
83+
wString.push('A', 'B', 'C', 'D');
84+
console.log('before: ', wString)
85+
86+
let charAtString = wString.chartAt(3);
87+
console.log('charAtString = ', charAtString);
88+
89+
let concatValueof = wString.concat('e', 'f', 1, undefined, null, 3, 0);
90+
console.log(' concatValueof = ', concatValueof)
91+

String/scharAt.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function WString() {
2+
this.value = []; // 初始化 wString对象 时,里面定义 一个空数组,将来用来装数据的容器
3+
this.length = 0; //因为是空数组,所以现在的长度是0
4+
/**
5+
* push()方法用于在数组的末端添加 数组的元素,并返回添加新元素后的数组长度。
6+
* 如果入参不合法,将会抛错;
7+
* 如果运行出错,将会返回空数组。
8+
* @param {...any} item1
9+
* @returns
10+
*/
11+
this.push = function (...item1) { // 当传入多个参数入来时 可用 ...形参名 表示
12+
13+
if (item1 == undefined || item1 == null || item1.length == 0) {
14+
throw new error('入参不合法');
15+
}
16+
for (let i = this.value.length, j = 0; j < item1.length; i++, j++) {
17+
this.value[i] = item1[j]
18+
}
19+
20+
this.length += item1.length;
21+
22+
return this.length;
23+
};
24+
25+
/**
26+
* charAt() 方法返回指定位置的字符,参数是从0开始编号的位置。
27+
* @param {*} item
28+
* @returns
29+
*/
30+
this.chartAt = function (item) {
31+
if (item == undefined || item == null || item > this.value.length || item < 0) {
32+
return this.value[0];
33+
}
34+
35+
for (let i = 0; i < this.value.length; i++) {
36+
if (i === item) {
37+
return this.value[i];
38+
}
39+
}
40+
41+
};
42+
43+
}
44+
45+
let wString = new WString();
46+
wString.push('A', 'B', 'C', 'D');
47+
console.log('before: ', wString)
48+
49+
let charAtString = wString.chartAt(3);
50+
console.log('charAtString = ', charAtString);
51+

String/sconcat.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function WString() {
2+
this.value = []; // 初始化 wString对象 时,里面定义 一个空数组,将来用来装数据的容器
3+
this.length = 0; //因为是空数组,所以现在的长度是0
4+
/**
5+
* push()方法用于在数组的末端添加 数组的元素,并返回添加新元素后的数组长度。
6+
* 如果入参不合法,将会抛错;
7+
* 如果运行出错,将会返回空数组。
8+
* @param {...any} item1
9+
* @returns
10+
*/
11+
this.push = function (...item1) { // 当传入多个参数入来时 可用 ...形参名 表示
12+
13+
if (item1 == undefined || item1 == null || item1.length == 0) {
14+
throw new error('入参不合法');
15+
}
16+
for (let i = this.value.length, j = 0; j < item1.length; i++, j++) {
17+
this.value[i] = item1[j]
18+
}
19+
20+
this.length += item1.length;
21+
22+
return this.length;
23+
};
24+
/**
25+
* concat() 方法用于 连接 两个字符串,返回一个新字符串,不改变原字符串
26+
*/
27+
this.concat = function (...concatStr) {
28+
let concatVal = this.value;
29+
for (let i = 0; i < concatStr.length; i++) {
30+
concatVal.push(concatStr[i])
31+
}
32+
let val = String(concatVal);
33+
console.log(val.length); //查看真实的长度
34+
return val;
35+
36+
}
37+
this.concat1 = function (...concatStr) {
38+
let concatVal = this.value;
39+
for (let i = 0; i < concatStr.length; i++) {
40+
concatVal.push(concatStr[i])
41+
}
42+
let val = concatVal.toString();
43+
let result = '';
44+
for (let j = 0; j < val.length; j++) {
45+
if (',' != val[j]) {
46+
result += val[j]
47+
}
48+
}
49+
return result;
50+
51+
}
52+
53+
}
54+
55+
let wString = new WString();
56+
wString.push('A', 'B', 'C', 'D');
57+
console.log('before: ', wString);
58+
59+
let concatValueof1 = wString.concat1('e', 'f', 1);
60+
console.log(' concatValueof = ', concatValueof1)
61+
62+
// let concatValueof = wString.concat('e', 'f');
63+
// console.log(' concatValueof = ', concatValueof)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function WString() {
2121

2222
return this.length;
2323
};
24+
25+
2426
}
2527

26-
let wString = new WString();
28+
let wString = new WString();
29+
wString.push('A', 'B', 'C', 'D');
30+
console.log('before: ', wString)

prototype/module.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function m1() {
2+
//...
3+
}
4+
5+
function m2() {
6+
//...
7+
}
8+
//”污染”了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关系。
9+
10+
var module1 = new Object({
11+
_count: 0,
12+
m1: function () {
13+
//...
14+
},
15+
m2: function () {
16+
//...
17+
}
18+
});
19+
//上面是模块写成一个对象,所有的模块成员都放到这个对象里面。
20+
//这样的写法会暴露所有模块成员,内部状态可以被外部改写。
21+
22+
23+
function StringBuilder() {
24+
let buffer = [];
25+
this.add = function (str) {
26+
buffer.push(str)
27+
};
28+
this.toString = function () {
29+
return buffer.join('')
30+
};
31+
}
32+
//这种方法将私有变量封装在构造函数中,违反了构造函数与实例对象相分离的原则。
33+
//并且,非常耗费内存。

prototype/prototype.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
// function Muqin() { };
2-
// let child = new Muqin();
3-
// child.constructor === Muqin;
4-
// Object.getPrototypeOf(child) === Muqin.prototype;
5-
6-
// function Nana() { };
7-
// let father = new Nana();
8-
// father.constructor === Nana;
9-
// Object.getPrototypeOf(father) === Nana.prototype;
10-
11-
12-
131
function Grandma() { };
142
let father = new Object();
153
Grandma.apply(father);
164
Object.setPrototypeOf(father, Grandma.prototype);
175
console.log(Object.getPrototypeOf(father) === Grandma.prototype);
186

197
function Mother() { };
20-
Mother.prototype = father;
8+
Mother.prototype = father; //创建Mother构造函数后立即将Mother的原型对象指向father
219

2210
let child = new Object();
2311
Mother.apply(child);

0 commit comments

Comments
 (0)