1. class的概念

定义一个class,属性都是private,方法都是public。

Hello.js:

使用class

index.js:

2. 单例类

使用exports而不是module.exports。区别在于Hello导出的是新的Function对象,而single导出的是single.js的初始对象{}。

属性都是private,方法都是public需要exports接口导出。

exports.name = name,相当于给接口{}创建动态属性。是无法导出var name的,因为name不是引用类型。

single.js:

new single()的话会报错。无论在哪里引用require single,值getName都是一样的。

index.js:

3. JS方法和变量的总结

1 对象的方法和属性。可理解为对象原本包含的变量和方法。

Hello.js

//模块封装Hello类
module.exports = Hello; //Hello类定义
function Hello(){
this.age = 1; this.setAge = function(age){
this.age = age;
} this.getAge = function (){
return this.age;
}
}

  

index.js

var Hello = require("./Hello.js");
var hello = new Hello(); //访问age
console.log(hello.age); //1
console.log(hello.getAge()); //1 //修改age
hello.setAge(2); //访问修改后的age
console.log(hello.age); //2
console.log(hello.getAge()); //2 //新的实例,age初始值不变
var hello2 = new Hello();
console.log(hello2.age); //1

  

  

 2 类方法和变量,可理解为类的静态变量和方法

Hello.js

//模块封装Hello类
module.exports = Hello; //Hello类定义
function Hello(){ } Hello.age = 1; Hello.getAge = function(){
return Hello.age;
}

 index.js

var Hello = require("./Hello.js");
var hello = new Hello(); //实例无法访问静态变量
console.log(hello.age); //undifined
//console.log(hello.getAge()); //报错 //访问age
console.log(Hello.age); //1
console.log(Hello.getAge()); //1 //修改age
Hello.age = 2; //访问修改后age
console.log(Hello.age); //2
console.log(Hello.getAge()); //2

 3 原型变量和方法,可理解为对象原本包含的变量和方法。可用于继承。

Hello.js

//模块封装Hello类
module.exports = Hello; //Hello类定义
function Hello(){ } Hello.prototype.age = 1; Hello.prototype.getAge = function(){
return Hello.prototype.age;
}

index.js

var Hello = require("./Hello.js");
var hello = new Hello(); //访问age
console.log(hello.age); //1
console.log(hello.getAge()); //1 //修改age
hello.age = 2; //访问修改后的age
console.log(hello.age); //2
console.log(hello.getAge()); //1 //新的实例
var hello2 = new Hello();
console.log(hello.age); //2
console.log(hello.getAge()); //1

  

4  类外的变量和方法 

Hello.js

//模块封装Hello类
module.exports = Hello; var age = 1;
var getAge2 = function(){
return age;
} //Hello类定义
function Hello(){ this.setAge = function(_age){
age = _age;
} this.getAge = function (){
return age;
}
}

index.js

var Hello = require("./Hello.js");
var hello = new Hello(); //访问age
console.log(hello.age); //undifined
console.log(hello.getAge()); //1 //无法访问getAge2()
//console.log(hello.getAge2()); //报错
//Hello.getAge2(); //报错 //修改age
hello.setAge(2); //访问修改后的age
console.log(hello.age); //undifined
console.log(hello.getAge()); //2 //新的实例
var hello2 = new Hello();
console.log(hello2.age); //undifined
console.log(hello2.getAge()); //2

  

总结:

1. 无法和java、c#那样有class extends implements private protected public static等来定义变量和方法。

2. 对于非前端js的使用者,nodejs的对象使用很变扭。

node中的对象的更多相关文章

  1. node中非常重要的process对象,Child Process模块

    node中非常重要的process对象,Child Process模块Child Process模块http://javascript.ruanyifeng.com/nodejs/child-proc ...

  2. node中的Stream-Readable和Writeable解读

    在node中,只要涉及到文件IO的场景一般都会涉及到一个类-Stream.Stream是对IO设备的抽象表示,其在JAVA中也有涉及,主要体现在四个类-InputStream.Reader.Outpu ...

  3. 深入理解jQuery、Angular、node中的Promise

    最初遇到Promise是在jQuery中,在jQuery1.5版本中引入了Deferred Object,这个异步队列模块用于实现异步任务和回调函数的解耦.为ajax模块.队列模块.ready事件提供 ...

  4. Node中的定时器详解

    在大多数的业务中,我们都会有一些需求,例如几秒钟实现网页的跳转,几分钟对于后台数据进行清理,node与javascript都具有将代码延迟一段时间的能力.在node中可以使用三种方式实现定时功能:超时 ...

  5. node中的流程控制中,co,thunkify为什么return callback()可以做到流程控制?

    前言 我在学习generator ,yield ,co,thunkify的时候,有许多费解的地方,经过了许多的实践,也慢慢学会用,慢慢的理解,前一阵子有个其他项目的同事过来我们项目组学习node,发现 ...

  6. c++中返回对象与返回引用的区别

    这几天在做用C++做课程设计,对其返回对象的实现感到迷惑. 通过对汇编代码的分析,可以清楚的看到,直接返回引用和返回对象的区别到底是什么. 分析的程序如下 #include<cstdio> ...

  7. Node.js全局对象

    Node.js的全局对象是具有全局性的,它们可在所有的模块中应用.我们并不需要包括这些对象在应用中,而可以直接使用它们.这些对象的模块,函数,字符串和对象本身,如下所述. __filename __f ...

  8. 使用express+multer实现node中的图片上传

    使用express+multer实现node中的图片上传 在前端中,我们使用ajax来异步上传图片,使用file-input来上传图片,使用formdata对象来处理图片数据,post到服务器中 在n ...

  9. 简单剖析Node中的事件监听机制(一)

    使用js的class类简单的实现一个事件监听机制,不同于浏览器中的时间绑定与监听,类似于node中的时间监听,并且会在接下来的文章中去根据自己的理解去写一下Event模块中的原理. Node.js使用 ...

随机推荐

  1. QML vs WEB

    UI领域, 如果是桌面应用,QML可以更快速.如果是手机UI,H5绝对占优. 移动设备已经为各行业的HMI的响应性和用户友好性设定了标准.汽车,医疗设备,工业自动化系统和消费电子产品制造商现在希望为其 ...

  2. spring boot 自学笔记(四) Redis集成—Jedis

    上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例. 修改Red ...

  3. laravel 控制器

    1:如何快速的创建一个控制器 用cmd进入当前的项目名文件夹里面执行如下语句: php artisan  make:controller  HgjController 2:编辑Hgj中的index方法 ...

  4. JavaScript 关键字快速匹配

    来源: http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html http://www.etherdream ...

  5. maven pom.xml 详解(注释版)

    转自:http://mrlee23.iteye.com/blog/1806412 pom.xml <project xmlns="http://maven.apache.org/POM ...

  6. The configuration file 'appsettings.json' was not found and is not optional

    问: .Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file ...

  7. HTML input只能输入数字

    onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" onafterpaste="this.value=this.v ...

  8. Oracle过程及函数的参数模式详解

    一.In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: (1)in模式 in模式是引用传 ...

  9. <img/>标签onerror事件在IE下的bug和解决方法

    IE下打开网页时,会弹出“Stack overflow at line: 0”的弹框.经分析,这个bug是由于img标签的onerror事件引起的.程序中用到的代码片段如下:正常情况下显示src所指路 ...

  10. sql 字符串操作

    SQL Server之字符串函数   以下所有例子均Studnet表为例:  计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student ...