js类的几种写法
我们常用的有以下几种方法来用JavaScript写一个“类”:
1. 构造函数(public属性和方法)
- 1: function Person(iName, iAge){
- 2: this.name=iName; //public
- 3: this.age=iAge; //public
- 4: this.ShowStudent=function(){ //public
- 5: alert(this.name);
- 6: };
- 7: }
缺点很明显,类的属性和方法,很容易被外部修改。
以上的属性和方法都是public的。下面的例子给出private和public的属性和方法。
2. 构造函数(public, private属性和方法)
- 1: function Person(iName, iAge){
- 2: //private field
- 3: var name = iName;
- 4: var age = iAge;
- 5:
- 6: //private method
- 7: var privatefn = function(){
- 8: alert(name);
- 9: }
- 10:
- 11: return {
- 12: //public field
- 13: Name: "hello " + name,
- 14: Age: "hello " + age,
- 15:
- 16: ShowStudent: function(){
- 17: privatefn();
- 18: alert(this.Name);
- 19: }
- 20: };
- 21: }
调用:(new Person("xiao","10")).ShowStudent();
3. 原型方法(prototype)
- 1: function c(){}
- 2: c.prototype={
- 3: name: "init value a",
- 4: setName: function(iName){
- 5: this.name=iName;
- 6: },
- 7: getName: function(){
- 8: alert('hello from c, name: ' + this.name);
- 9: }
- 10: };
- 11: (new c).getName(); // 输出hello from c, name: init value a
4. 构造函数+原型方法(prototype)
- 1: function Person(iName) {
- 2: this.name = iName;
- 3: };
- 4:
- 5: Person.prototype={
- 6: getName: function(){
- 7: return this.name;
- 8: }
- 9: };
- 10:
- 11: //调用
- 12: var b = new Person("jack");
- 13: alert(b.getName());
一般多会用上面这种写法。
5. 构造函数+原型方法(prototype)- 节省内存的写法
- 1: function Person(iName, iAge){
- 2: this.name=iName;
- 3: this.age=iAge;
- 4:
- 5: //对象实例都共享同一份方法不造成内存浪费
- 6: if(typeof Person._initialized == "undefined"){
- 7: Person.prototype.ShowStudent=function(){
- 8: alert(this.name);
- 9: };
- 10: Person._initialized=true;
- 11: }
- 12: }
- 13: //调用
- 14: (new Person("jack","20")).ShowStudent();
以上的实现方法如果不用_initialized的方法,也可以指向一个外部函数,道理一样。
6. JavaScript类的单例(Singleton)模式写法
- 1: var MyNamespace = {};
- 2: MyNamespace.Singleton = (function() {
- 3: var uniqueInstance; // Private attribute that holds the single instance.
- 4: function constructor() { // All of the normal singleton code goes here.
- 5: // Private members.
- 6: var privateAttribute1 = false;
- 7: var privateAttribute2 = [1, 2, 3];
- 8: function privateMethod1() {
- 9: //...
- 10: }
- 11: function privateMethod2(args) {
- 12: //...
- 13: }
- 14: return { // Public members.
- 15: publicAttribute1: true,
- 16: publicAttribute2: 10,
- 17: publicMethod1: function() {
- 18: // ...
- 19: },
- 20: publicMethod2: function(args) {
- 21: // ...
- 22: }
- 23: }
- 24: }
- 25: return {
- 26: getInstance: function() {
- 27: if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.
- 28: uniqueInstance = constructor();
- 29: }
- 30: return uniqueInstance;
- 31: }
- 32: }
- 33: })();
- 34:
- 35: //调用:
- 36: MyNamespace.Singleton.getInstance().publicMethod1();
js类的几种写法的更多相关文章
- Python 自定义元类的两种写法
有关元类是什么大家自己搜索了解,我这里写一下实现元类的两种写法 # 自定义元类 #继承type class LowercaseMeta(type): ''' 修改类的属性名称为小写的元类 ''' # ...
- [JS] 面向对象的5种写法和拓展JS对象的写法
面向对象的JAVA 最开始当然是对象的定义了 收集了以下的五种写法 //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; C ...
- JS面向对象的几种写法
JS 中,面向对象有几种写法.归纳下,大概有下面这几种:工厂模式,构造函数模式,原型模式,构造函数与原型模式的混合使用,原型链继承,借用构造函数继承. 一.工厂模式 function person ( ...
- js面向对象的五种写法
第一种: //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = functio ...
- (转载)无缝滚动图片的js和jquery两种写法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS 定时器的4种写法及介绍
JS提供了一些原生方法来实现延时去执行某一段代码,下面来简单介绍一下setTiemout.setInterval.setImmediate.requestAnimationFrame. 一.什么是定时 ...
- js和jquery 两种写法 鼠标经过图片切换背景效果
这个是javascript的写法 <img src="res/img/shop-c_32.jpg" alt="" onmouseover="th ...
- Struts2中Action类的三种写法
一.普通的POJO类(没有继承没有实现)-基本不使用 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创 ...
- 3.Struts2中Action类的三种写法
一.普通的POJO类(没有继承没有实现)-基本不使用 public class DemoAction1 { public String execute(){ System.out.println(&q ...
随机推荐
- UP UP UP!(dp)
UP UP UP! Time Limit: 1 Sec Memory Limit: 128 MB Submit: 92 Solved: 27SubmitStatusWeb Board Descri ...
- Java算法简介及排序剖析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 从小白晋升,一路走来:从helloworld,到JFrame,再到Android:从城外小子,到内城 ...
- 走进C++程序世界------继承和派生
继承和派生 继承是面向对象编程语言的最重要方面之一,正确的使用继承可编写出设计良好,容易于维护和扩展的应用程序.下面是在其他博客中的总结: ****************************** ...
- CentOS7安装mysql5.6.23 -(转)
一.下载glibc版本的Mysql mysql-advanced-5.6.23-linux-glibc2.5-x86_64.zip 解压后,得到 mysql-advanced-5.6.23-linux ...
- 第四章 Linux环境
程序参数 main函数声明: int main(int argc, char *argv[]) 其中argc是接受到的参数个数,argv是存储参数的字符串数组. 或者声明为: main() 这样也 ...
- BAK文件怎么恢复到数据库中
1.右击SQLServer2000实例下的“数据库”文件夹.就是master等数据库上一级的那个图标.选择“所有任务”,“还原数据库” 2.在“还原为数据库”中填上你希望恢复的数据库名字.这个名字应该 ...
- volatile 和const 变量的使用
一.volatile定义: 一个定义为volatile的变量是说这变量可能会被意想不到的被改变,这样,有了volatile变量后,就提醒编译器就不会去假设这个变量的值了.精确地说就是,编译中的优化器在 ...
- mysql插入返回当前生成的主键
1:sql中需要添加属性 keyColumn="base_price_id" keyProperty="basePriceId" useGeneratedKe ...
- Android 贝塞尔曲线
博客图片备份位置:
- [转]Linux netstat命令详解
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...