1.闭包封装。在这个封装方法中,所有的实例成员都共享属性和方法, 使得所有得方法和属性都私有且对象间共享

(function ($) {
var Person = function(name) {
return new Person.fn.init(name);
} Person.fn = Person.prototype = {
constructor: Person,
init: function(name) {
this.name = name;
this.sayHello = function() {
this.makeArray();
}
},
makeArray: function() {
console.log(this.name);
}
} Person.fn.init.prototype = Person.fn; return Person;
})(jQuery);

2.对象原型封装

function Person(name,age,no){
this._name=name;
this._age=age; this._no=no;
this.checkNo=function(no){
if(!no.constructor == "string"|| no.length!=4)
throw new Error("学号必须为4位"); };
//var _no,_age,_name;
this.setNo=function(no){
this.checkNo(no); this._no=no;
}; this.getNo=function(){
return this._no; };
this.setName=function(name){ this._name=name; }; this.getName=function(){ return this._name; }; this.setAge=function(age){ this._age=age; }; this.getAge=function(){ return this._age; }; this.setNo(no); this.setName(name); this.setAge(age);}Person.prototype={ toString:function(){ return"no = " + this.getNo() + " , name = " + this.getName() + " , age = " + this.getAge(); }};var per=new Person("lili",23,"0004");sconsole.log(per.toString());per.setNo("0001");console.log(per.toString());per.setAge(25);console.log(per.toString());

JavaScrpt常用的封装方法的更多相关文章

  1. iOS常用的封装方法

    做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...

  2. 常用DBhelper封装方法

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. web前端常用的封装方法

    1.放大镜 //页面加载完毕后执行 window.onload = function () { var oDemo = document.getElementById('demo'); var oMa ...

  4. Lua常用封装方法

    Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...

  5. 封装常用的selenium方法

    package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...

  6. 在项目中常用的JS方法封装

    使用方法简单,只需要放在你的 utils.js 工具文件中,直接export const 加上下面封装方法,在别的文件中使用 {方法1,方法2,方法3...}引用后直接使用即可. 01.输入一个值.返 ...

  7. AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇

    之前完成了 AppDelegate减负之常用三方封装 - 友盟推送篇: http://www.cnblogs.com/zhouxihi/p/7113511.html 今天接着来完成 - 友盟分享和三方 ...

  8. 【终结版】C#常用函数和方法集汇总

    C#里面的常用的函数和方法非常重要,然而做题的时候会经常忘记这些封装好的方法,所以我总结一下 C#常用函数和方法集. [1]C#操作字符串的常用使用方法 在 C# 中,您可以使用字符数组来表示字符串, ...

  9. MP实战系列(十二)之封装方法详解(续二)

    继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后. 此次要讲的是关于查询. 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事. 1.selectB ...

随机推荐

  1. Pocket Gem OA: Log Parser

    time a given player spends actually connected to the network. We keep console logs of various game s ...

  2. aos.css 动画效果

    aos网址 https://codepen.io/michalsnik/pen/WxNdvq <div class="item" data-aos="fade-up ...

  3. Unity如何退出游戏

    使用 Application.Quit(),但在 editor 模式下使用 Application.Quit()是没用的,要用 EditorApplication.isPlaying = false. ...

  4. centos上发布部署python的tornado网站项目完整流程

    先说下大体上的做法,开发环境上要新弄一个 virtualenv的环境,在这个里面放你的开发调试,当然这个其实也不是必须的,但是这样会方便管理一些. 再在centos上也弄一个 virtualenv虚拟 ...

  5. sql server导出数据,远程连接失败,需要设置权限

    在sql  server management中右键当前连接——>方面 在 服务器配置中 将  RemoteAccessEnabled.RemoteDacEnabled设置为TRUE 安全性—— ...

  6. redis 通用函数

    redis 通用函数 construct 命令/方法/函数 Description Creates a Redis client 创建一个Redis客户端 Example $redis = new R ...

  7. SynchronousQueueDemo

    1.ArrayDeque, (数组双端队列) 2.PriorityQueue, (优先级队列) 3.ConcurrentLinkedQueue, (基于链表的并发队列) 4.DelayQueue, ( ...

  8. [转载]URL 源码分析

    URI 引用包括最多三个部分:模式.模式特定部分和片段标识符.一般为: 模式:模式特定部分:片段 如果省略模式,这个URI引用则是相对的.如果省略片段标识符,这个URI引用就是一个纯URI. URI是 ...

  9. html禁止页面滚动

    <div @touchmove.prevent></div> @touchmove.prevent   //加到标签上禁止滚动

  10. Literal绑定数据

    前台: <asp:Literal ID = "ChiCunShow" runat = "server"></asp:Literal> 后 ...