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之.jdk卸载-Linux
Java之.jdk卸载-Linux 卸载Linux自带的jdk 首先查询: # rpm -qa | grep jdk 使用root账户,进行卸载: # yum -y remove xxxxxxxx( ...
- flask框架中,利用数据库增删查改
# 配置数据库app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:mysql@127.0.0.1:3306/booktest" ...
- windows ip路由
windows 20082块网卡,连接远程mysql数据库一直不通,ping正常,telnet 3306端口不正常 route print 路由情况 route add 10.255.2574.XXX ...
- archlinux中安装Oracle12c的过程中遇到的问题
INFO: : cannot find INFO: /usr/lib64/libpthread_nonshared.aINFO: INFO: genclntsh: Failed to link lib ...
- jQuery常见用法
jQuery有好多版本本,无法同时引用两个不同的版本,容易造成混乱,用哪一个,调用哪一个.\ jQuery引用到<head></head>中,页面加载时就需要特效调用这些方法. ...
- django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
报错环境 python=3.6.5,django=2.2,PyMySQL=0.9.3 …… django.core.exceptions.ImproperlyConfigured: mysqlcl ...
- Oarcle之事务
update:更新 例如转账: update emp_ temp set sal = sal-500 where ename = 'JONES':(更新表中sal项 为sal-500 是当ename= ...
- spark-jobserver安装实践 (centos7.4)
spark-jobserver 提供了一个RESTful接口来提交和管理spark的jobs,jars和job contexts. 该工程位于:https://github.com/spark-job ...
- c# 设计模式(一) 工厂模式
源代码在github上面,需要的自己进行下载:https://github.com/yuzhoukamen/UnikmDesignPattern.git 工厂模式(Factory Pattern)是最 ...
- redis windows 安装流程
https://blog.csdn.net/u012343297/article/details/78839063 1,redis官方下载地址:https://redis.io/download,re ...