JavaScript创建类的三种方式

//第一种  创建类方法。
// 用方法模拟 构造函数。
function classobj() {
    this.name = 'xiaoming';
}
classobj.text = 'text';
//创建实例对象
var obj = new classobj();
console.log(obj);


//第二种创建类方法
//这种方法比"构造函数法"简单,但是不能实现私有属性和私有方法,实例对象之间也不能共享数据,对"类"的模拟不够全面。
var classtwo = {
    nametwo: 'sunzhenyong',
    f: function () {
        alert(1);
    }
};
var objtwo = Object.create(classtwo);
//alert(objtwo.nametwo);

// 封装一个类
var classthree = {
    createclass: function () {
        var cat = {};
        cat.namethree = 'three',
        cat.cry = function () { alert('miaomiao'); }
        return cat;
    }
};
var objthree = classthree.createclass();
//alert(objthree.namethree);
// 继承效果
var classfour = {
    createclass: function () {
        var cat = classthree.createclass();
        cat.namefour = 'foure';
        return cat;
    }
}
var objfour = classfour.createclass();
alert(objfour.namethree);
alert(objfour.namefour);

原文链接:

http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html

原文地址:https://www.cnblogs.com/sunzhenyong/p/4062779.html