惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - Zhuang miao

使用bootstrap和metroui设计的微网站或手机app界面 利用Mahout实现在Hadoop上运行K-Means算法 大连二手汽车培训网上线 淘宝开放平台API调用nodejs实现 Nodejs+express+angularjs+mongodb搭建前端项目框架NJBlog 淘宝UED前端智勇大冲关第二季 CC.net&Nant配置文件 介绍一个款可以在javascript对象上实现观察者模式的类库-Watch.js 开发nodejs模块并发布到npm的简单示例 用jsTestDriver运行jasmine cases 使用Jasmine测试你的Javascript(三)之 Matchers 使用Jasmine测试你的Javascript(二)之 Suites和specs 使用Jasmine测试你的Javascript(一)之 Jasmine简介 用SignalR创建实时永久长连接异步网络应用程序 Javascript实现图片的预加载的完整实现 常见前端面试题【转】 更改页面背景的jquery插件 12款华丽的Admin管理后台模板 AOP
javascript面向对象中的对象创建、继承、封装等实现方式
Zhuang miao · 2012-10-26 · via 博客园 - Zhuang miao

2012-10-26 15:01  Zhuang miao  阅读(268)  评论()    收藏  举报

 *包含私有成员的对象创建方法
 * @param bname
 * @constructor
 */

var Book =function(bname){
    var name,isbn;
    this.GetName=function(){
        return name;
    };
    this.GetISBN=function(){
        return isbn;
    };
    this.SetName=function(newname){
        name=newname;
    };
    this.SetISBN=function(newisbn){
        isbn=newisbn;
    };
    this.SetName(bname);
 }
Book.prototype={
    Display:function(){
       return "Name:"+this.GetName()+ " ISBN:"+this.GetISBN();
    }
}
Book.prototype.FormatDisplay=function(){
    return "Name:"+this.GetName()+ ", ISBN:"+this.GetISBN();
    };

/**
 *包含静态成员的创建对象的方法
 *CheckIsbn只被实例化一次
 * var book=new Book("");
*/

var Book=(function(){
    var numOfBook=0;
    function CheckIsbn(isbn){
       return isbn.match("\\d(3)-\\d(5)");
    };
    var ctr= function(bname){
        var name,isbn;
        this.GetName=function(){
            return name;
        };
        this.GetISBN=function(){
            return isbn;
        };
        this.SetName=function(newname){
            name=newname;
        };
        this.SetISBN=function(newisbn){
            if(CheckIsbn(newisbn)) isbn=newisbn;
        };
        numOfBook++;
        if(numOfBook>10) throw new Error("Book: Only 10 instances of Book can be Created!");

        this.SetName(bname);
    };
    ctr.GetNumber=function(){
        return numOfBook;
    }
    return ctr;
})();

/**
 * 类式继承的实现
 * @param subClass
 * @param superClass
 
*/

function extend(subClass, superClass){
    var  F=function(){};
    F.prototype=superClass.prototype;
    subClass.prototype=new F();
    subClass.prototype.constructor=subClass;

    subClass.superclass =superClass.prototype;
    if(superClass.prototype.constructor==Object.prototype.constructor){
        superClass.prototype.constructor=superClass;
    }
}

function Person(name){
    this.name=name;
};
Person.prototype.getName=function(){
    return this.name;
};

function author(name,book){
    author.superclass.constructor.call(this,name);
    this.book=book;
};

extend(author,Person);

author.prototype.getBooks=function(){
    return this.book;
}
author.prototype.getName=function(){
    var name=author.superclass.getName.call(this);
    return name+ this.getBooks();
};

/**
 * 原型式继承
 * @type {Object}
 
*/
var Person = {
    name:'default',
    getName:function(){
        return this.name;
    }
};
function clone(cloneClass){
    var F=function(){};
    F.prototype=cloneClass;
    return new F();
}
var reader=clone(Person);
reader.getName();

/**
 * 掺元类,通过扩充的方式让类共享函数
 * @param receivingClass
 * @param givingClass
 * @param params
 
*/
function augment(receivingClass,givingClass,params){
    if(params){
        console.log('传进来的参数为:'+params.join(','));
        for(var i= 0,len=params.length;i<len;i++){
            receivingClass.prototype[params[i]]=givingClass.prototype[params[i]];
        }
    }
    else{
    for (var methodName in givingClass.prototype) {
        if(!receivingClass.prototype[methodName]){
            receivingClass.prototype[methodName]=givingClass.prototype[methodname];
        }
    }
    }
};

var Mixin=function(){};
Mixin.prototype={
    serialize:function(){
        var output=[];
        for(key in this){
            output.push(key+":"+this[key]);
        }
        return output.join(',');
    },
    wMsg:function(){
         alert("Welcome");
    }
};
function author(name){this.name=name};

var a=new author("miaow");

augment(author,Mixin,['serialize','wMsg']);