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;
    })();
  • 相关阅读:
    CSS性能让JavaScript变慢?
    Cordova优缺点与环境部署
    nodeapi
    git常用命令
    常见状态码
    关于拉萨
    英语学习
    SQL 按表中的一个int值拆分成对应的个数的记录条数
    SQL分组编号
    C#四舍五入
  • 原文地址:https://www.cnblogs.com/yejianyong/p/9197653.html
Copyright © 2011-2022 走看看