zoukankan      html  css  js  c++  java
  • js封装

    常规封装

    function Person (name,age,sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    
    Pserson.prototype = {
        constructor:Person,
        sayHello:function(){
            console.log('hello');
        }
    }

    闭包封装
    var Person = (function(){
    //静态私有属性方法
    var home = "China";
    function sayHome(name)
    {
    console.log(name + "'s home in " + home);
    }
    //构造函数
    function _person(name, age){
    var _this = this;
    //构造函数安全模式,避免创建时候丢掉new关键字
    if(_this instanceof _person){
    //共有属性, 方法
    _this.name = name;
    _this.getHome = function(){
    //内部访问私有属性,方法
    sayHome(_this.name);
    };
    _this.test = sayHome; //用于测试 //构造器
    _this.setAge = function(age){ _this.age = age + 12;
    }(age);
    }
    else{ return new _person(name, age); }
    }
    //静态共有属性方法
    _person.prototype = {
    constructor: _person,
    drink: "water",
    sayWord: function(){
    console.log("ys is a boy");
    } }
    return _person;
    })();
  • 相关阅读:
    常见优化函数
    排序算法
    小米编程题
    leetcode 刷题
    beam_search 和 viterbi算法的区别
    快速排序
    vitrebi算法进行分词的案例
    python 进行视频剪辑
    keras实现MobileNet
    HMM、CTC、RNN-T训练时所有alignment的寻找方法
  • 原文地址:https://www.cnblogs.com/yejianyong/p/9197653.html
Copyright © 2011-2022 走看看