zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    hdu 3746 Cyclic Nacklace
    hdu 3336 Count the string
    hdu 1358 Period
    hiho一下 第一周 最长回文子串
    KMP算法详解
    Java 之 static的使用方法(6)
    Jave 之方法-函数(5)
    Java 之数组(4)
    大数据-storm学习资料视频
    大数据-spark-hbase-hive等学习视频资料
  • 原文地址:https://www.cnblogs.com/sunzhenyong/p/4062779.html
Copyright © 2011-2022 走看看