JavaScrpt常用的封装方法
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常用的封装方法的更多相关文章
- iOS常用的封装方法
做开发也有一段时间了,看了好多大神的代码,总体感觉他们写的代码简洁,好看,然而在对比下我写的代码,混乱,无序,简直不堪入目啊! 总体来说大神们的代码封装的都比较好,对一个项目要重复用到的代码他们都会封 ...
- 常用DBhelper封装方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- web前端常用的封装方法
1.放大镜 //页面加载完毕后执行 window.onload = function () { var oDemo = document.getElementById('demo'); var oMa ...
- Lua常用封装方法
Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...
- 封装常用的selenium方法
package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...
- 在项目中常用的JS方法封装
使用方法简单,只需要放在你的 utils.js 工具文件中,直接export const 加上下面封装方法,在别的文件中使用 {方法1,方法2,方法3...}引用后直接使用即可. 01.输入一个值.返 ...
- AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇
之前完成了 AppDelegate减负之常用三方封装 - 友盟推送篇: http://www.cnblogs.com/zhouxihi/p/7113511.html 今天接着来完成 - 友盟分享和三方 ...
- 【终结版】C#常用函数和方法集汇总
C#里面的常用的函数和方法非常重要,然而做题的时候会经常忘记这些封装好的方法,所以我总结一下 C#常用函数和方法集. [1]C#操作字符串的常用使用方法 在 C# 中,您可以使用字符数组来表示字符串, ...
- MP实战系列(十二)之封装方法详解(续二)
继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后. 此次要讲的是关于查询. 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事. 1.selectB ...
随机推荐
- JAVA String中文乱码
System.out.println(str); String str1 = new String(str.getBytes("ISO-8859-1"), "utf-8& ...
- 压力(性能)测试及jmeter的使用
github中标注lab3的文档记录了我的详细的实验过程,有关环境搭建.过程理解.实验结果与遇到的问题等,希望可以帮到初学者. 欢迎交流- https://github.com/fogmisty/So ...
- zabbix监控实战<1>
第一章 监控家族 1.1 为什么选择监控? 因为在一个IT集群中或者是一个大环境中,包括各种硬件设备.软件设备等系统的构成也是极其复杂的. 多种应用构成负载的IT业务系统,保证这些资源的正常运转,是一 ...
- puppeteer(五)chrome启动参数列表API
List of Chromium Command Line Switches https://peter.sh/experiments/chromium-command-line-switches/ ...
- mvc jquery ajax传递数组null问题
mvc jquery ajax传递数, areaIds是个int数组.后台action用list<int>接收.当我想传空值时,先用null传递,结果action收到的AreaIds竟然 ...
- EXCEL对比在职员工与离职员工
EXCEL 在B1中 填写这个 =VLOOKUP(A1,C:C,1,0) 然后往下拉 只要有出现#N/A 说明已经离职了 公司需要
- delphi 的一些注意点和知识点
关于Delphi中产生的文件 编辑阶段: pas/单元文件,dpk/组件包文件,dpr/工程文件,dfm/窗体文件 编译阶段: dcu/单元编译文件,dcp/Delphi Compile ...
- sitecore8.2 基于站点来查询item
树形结构: /sitecore /content /foo-site /home /my-account /bar-site /home /my-account /baz-site /home /my ...
- node获取当前路径的三种方法
node提供了3种获取路径的方法 ,在当前目录下,运行node const {resolve} = require('path') console.log('__dirname : ' + __di ...
- 按下回车默认提交form表单问题
最近开发中碰到一个问题,项目中有几个列表展示页面,允许用户通过查询条件模糊查询数据.用户录入关键字后点击回车会调用查询方法根据关键字查询,原先功能没有问题,但是最近发现在查询输入框中按下回车会直接刷新 ...