Skip to content

Commit 0196fdf

Browse files
committed
js面向对象编程学习中
1 parent 232ed81 commit 0196fdf

10 files changed

Lines changed: 255 additions & 0 deletions

File tree

‎fotm-html-http/easyserver.js‎

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var check=function(){};
2+
//<1>
3+
check.add=function(){
4+
console.log("add");
5+
}
6+
check.del=function(){
7+
console.log("del");
8+
}
9+
10+
11+
//check.add();
12+
//<2>
13+
var newCheck=function(){
14+
//返回一个对象
15+
return {
16+
add:function(){
17+
console.log("add");
18+
},
19+
del:function(){
20+
console.log("del");
21+
}
22+
}
23+
}
24+
25+
//<3>
26+
function Check(){
27+
this.add=function(){
28+
this.del();
29+
console.log("add");
30+
}
31+
32+
this.del=function(){
33+
console.log("del");
34+
}
35+
}
36+
37+
//<4>使用原型链
38+
function CheckTwo(){
39+
40+
}
41+
42+
CheckTwo.prototype={
43+
add:function(){
44+
console.log("add");
45+
},
46+
del:function(){
47+
console.log("del");
48+
}
49+
}
50+
/* 第一部分的调用
51+
var x=new check();
52+
53+
x.add(); */
54+
55+
/* 第二部分的调用这里check与newCheck没有任何的关系,虽然使用了关键字new,但并不是真正意义上的类
56+
var check=new newCheck();
57+
check.add(); */
58+
59+
/* 第三部分的调用
60+
面向对象的写法,但是通过第三种方式创建的对象,都具有相同的方法和属性
61+
很消耗内存
62+
var check1=new Check();
63+
var check2=new Check();*/
64+
65+
66+
67+
/* 调用的方式与上面几种是相同的,但是通过这种方式创建的对象拥有相同的方法
68+
因为他们都要依赖原型链上去找方法和属性,创建的对象都绑定在checkTwo的原型上。 */
69+
70+
71+
72+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//给每个函数都添加一个方法<1>
2+
Function.prototype.add=function(){
3+
console.log("add");
4+
}
5+
6+
/*不允许使用第一种方式
7+
因为污染了原生的Funtion,别人创建的函数被你创建的函数污染
8+
var x=function(){};
9+
x.add(); */
10+
11+
Function.prototype.addMethod=function(name,fn){
12+
this[name]=fn;
13+
//为了链式调用
14+
return this;
15+
}
16+
17+
18+
var x=function(){};
19+
20+
//链式添加方法
21+
x.addMethod("add",function(){
22+
console.log("add1");
23+
return this;
24+
}).addMethod("del",function(){
25+
console.log("del");
26+
return this;
27+
});
28+
29+
x.add().del();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function Book(id,name,page){
2+
this.id=id;
3+
this.name=name;
4+
this.page=page;
5+
}
6+
7+
Book.prototype.display=function(){
8+
console.log("display");
9+
}
10+
11+
Book.Name="wangmazi1";
12+
13+
var x=new Book();
14+
x.Name;
15+

‎高级的Node/first/index.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var CountStream = require("./streamcounts");
2+
3+
var countStream = new CountStream("book");
4+
5+
var http = require("http");
6+
7+
http.get("http://localhost:3000", function (res) {
8+
res.pipe(countStream);
9+
})
10+
11+
countStream.on("total", function (count) {
12+
console.log("Total match:" + count);
13+
})

‎高级的Node/first/npm-debug.log‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ 'D:\\1.安装软件\\NodeJs\\node.exe',
3+
1 verbose cli 'D:\\1.安装软件\\NodeJs\\node_modules\\npm\\bin\\npm-cli.js',
4+
1 verbose cli 'search',
5+
1 verbose cli 'express' ]
6+
2 info using npm@3.10.10
7+
3 info using node@v6.10.3
8+
4 silly mapToRegistry name -/all
9+
5 silly mapToRegistry using default registry
10+
6 silly mapToRegistry registry https://registry.npmjs.org/
11+
7 silly mapToRegistry data Result {
12+
7 silly mapToRegistry raw: '-/all',
13+
7 silly mapToRegistry scope: null,
14+
7 silly mapToRegistry escapedName: null,
15+
7 silly mapToRegistry name: null,
16+
7 silly mapToRegistry rawSpec: '-/all',
17+
7 silly mapToRegistry spec: '-/all',
18+
7 silly mapToRegistry type: 'local' }
19+
8 silly mapToRegistry uri https://registry.npmjs.org/-/all
20+
9 info updateIndex C:\Users\Administrator\AppData\Roaming\npm-cache\registry.npmjs.org\-\all\.cache.json
21+
10 verbose correctMkdir C:\Users\Administrator\AppData\Roaming\npm-cache correctMkdir not in flight; initializing
22+
11 warn Building the local index for the first time, please be patient
23+
12 silly update-index fetching https://registry.npmjs.org/-/all
24+
13 verbose request uri https://registry.npmjs.org/-/all
25+
14 verbose request no auth needed
26+
15 info attempt registry request try #1 at 16:57:10
27+
16 verbose request id 4f8bd578020089c2
28+
17 http request GET https://registry.npmjs.org/-/all
29+
18 info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
30+
19 info attempt registry request try #2 at 16:57:32
31+
20 http request GET https://registry.npmjs.org/-/all
32+
21 info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
33+
22 info attempt registry request try #3 at 16:58:43
34+
23 http request GET https://registry.npmjs.org/-/all
35+
24 verbose stack Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
36+
24 verbose stack at errnoException (dns.js:28:10)
37+
24 verbose stack at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
38+
25 verbose cwd E:\1.NodeStudy\高级的Node\first
39+
26 error Windows_NT 6.1.7601
40+
27 error argv "D:\\1.安装软件\\NodeJs\\node.exe" "D:\\1.安装软件\\NodeJs\\node_modules\\npm\\bin\\npm-cli.js" "search" "express"
41+
28 error node v6.10.3
42+
29 error npm v3.10.10
43+
30 error code ENOTFOUND
44+
31 error errno ENOTFOUND
45+
32 error syscall getaddrinfo
46+
33 error network getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
47+
34 error network This is most likely not a problem with npm itself
48+
34 error network and is related to network connectivity.
49+
34 error network In most cases you are behind a proxy or have bad network settings.
50+
34 error network
51+
34 error network If you are behind a proxy, please make sure that the
52+
34 error network 'proxy' config is set properly. See: 'npm help config'
53+
35 verbose exit [ 1, true ]

‎高级的Node/first/package.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "first",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"author": "",
10+
"license": "ISC"
11+
}

‎高级的Node/first/server.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var http = require("http");
2+
3+
http.createServer(function (req, res) {
4+
var html = "<body><h1>book</h1><h1>book</h1><h1>book</h1><h1>book</h1><h1>book</h1></body>";
5+
res.writeHead(200, {
6+
"Content-Type": "text/html;charset=utf-8"
7+
});
8+
res.end(html)
9+
}).listen(3000, function () {
10+
console.log("server is running!!");
11+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var Writable = require("stream").Writable;
2+
3+
var util = require("util");
4+
5+
module.exports = CountStream;
6+
7+
util.inherits(CountStream, Writable);
8+
9+
10+
function CountStream(matchText, options) {
11+
Writable.call(this,options);
12+
this.count=0;
13+
this.matcher=new RegExp(matchText,'ig');
14+
}
15+
16+
CountStream.prototype._write = function (chunk, encoding, callback) {
17+
var matchs = chunk.toString().match(this.matcher);
18+
if (matchs) {
19+
this.count += matchs.length;
20+
}
21+
callback();
22+
}
23+
24+
CountStream.prototype.end = function () {
25+
this.emit("total", this.count);
26+
}
27+

‎高级的Node/first/test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var assert = require("assert");
2+
3+
var CountStream = require("./streamcounts");
4+
5+
var countstream = new CountStream("example");
6+
7+
8+
9+
var fs = require("fs");
10+
11+
var passed = 0;
12+
13+
14+
countstream.on("total", function (count) {
15+
assert.equal(count, 1);
16+
passed++;
17+
})
18+
19+
console.log(__filename);
20+
fs.createReadStream(__filename).pipe(countstream);
21+
22+
process.on("exit", function () {
23+
console.log("count:"+passed);
24+
});

0 commit comments

Comments
 (0)