Js面向对象编程
Js面向对象编程
1. 什么是面向对象编程?
我也不说不清楚什么是面向对象,反正就那么回事吧。
编程有时候是一件很快乐的事,写一些小游戏,用编程的方式玩游戏等等
2. Js如何定义一个对象
一般变量的定义方法
var name = '小明'; var email = 'xiaoming@chinaedu.net'; var website = 'http://chinaedu.net'; |
写成对象的方式:
var xiaoming = { name : '小明', email : 'xiaoming@chinaedu.net', website : 'http://chinaedu.net' } |
访问属性的方式
xiaoming.name xiaoming['name'] |
追加属性
xiaoming.sex = '男'; |
给对象定义方法
var xiaoming = { name : '小明', email : 'xiaoming@chinaedu.net', website : 'http://chinaedu.net', sayHello : function(){ alert(this.name + ',你好!'); } } // 再追加一些属性和方法 xiaoming.birthday = '2005-09-23'; xiaoming.eat = function(){ alert(this.name + ',你妈叫你回家吃饭!'); }; |
移除属性的方法
delete xiaoming.sex |
检查对象是否存在某个属性
if('sex' in xiaoming){ alert("true"); } |
js对象的其实没有私有属性的,他的属性都是公有的。
一般对属性名前加_表示私有属性。
3. 对象的继承
我对继承的简单理解:子对象拥有父对象的属性,并可以对父对象的属性进行覆盖或者扩展。
第一种做法,直接把子对象的属性copy给父对象里,生成新的对象就是继承的效果了。
实现方法
function deepCopy(p, c) { var c = c || {}; for (var i in p) { if (typeof p[i] === 'object') { c[i] = (p[i].constructor === Array) ? [] : {}; deepCopy(p[i], c[i]); } else { c[i] = p[i]; } } return c; } |
4. js中如何定义一个类?
定义的function就是一个构造方法也就是说是定义了一个类;用这个方法可以new新对象出来。
function Person(name, age){ this.name = name; this.age = age; this.showInfo = function(){ alert(this.name + this.age + "岁"); } } Person p1 = new Person('小明', 17); |
5. 类的静态属性及方法
严格来说,ECMAScript 并没有静态作用域。不过,它可以给构造函数提供属性和方法。还记得吗,构造函数只是函数。函数是对象,对象可以有属性和方法。
Person.newOrder = 0; Person.newOrder = function(){ return Person.newOrder ++; } |
6. 类的继承
对象冒充
function Rect(width, height){ this.width = width; this.height = height; this.area = function(){return this.width*this.height;}; } function MyRect(width, height, name){ // this.newMethod = Rect; // this.newMethod(width,height); // delete this.newMethod; Rect.call(this,width,height);// Rect.apply(this, arguments); this.name = name; this.show = function(){ alert(this.name+" with area:"+this.area()); } } |
原型链(prototype chaining)
function Rect(){ } Rect.prototype = { width:10, height : 10, area : function(){return this.width*this.height;} }; function MyRect(name){ this.name = name; this.show = function(){ alert(this.name + " with area:" + this.area()); } } MyRect.prototype = new Rect(); |
混合型
function Rect(width,height){ this.width = width; this.height = height; } Rect.prototype = { area : function(){return this.width*this.height;} }; function MyRect(name, width, height){ Rect.call(this, width, height); this.show = function(){ alert(this.name + " with area:" + this.area()); } } MyRect.prototype = new Rect(); |
7. 可变参数
Js的方法没有重载的概念,但是他的方法参数是可变的。你可传n多参数,只看方法名,方法名相同就认为是一个方法。同名的方法只会覆盖之前的方法。
var Rect = function(){}; Rect.prototype = { _width : 5, _height : 10, width : function(width) { if (arguments.length > 0) { this._width = width; return this; } return this._width; }, height : function(height) { if (arguments.length > 0) { this._height = height; return this; } return this._height; } } var r1=new Rect(); r1.width(30).height(45); alert(r1.width() + "||" + r1.height()); |
8. 命名空间
定义一个edu.fn的命名空间
if (typeof Edu == 'undefined') { var Edu = {}; } if (typeof Edu.fn == 'undefined') { Edu.fn = {}; } (function($) { Edu.fn.core = { } })(jQuery); |
工具方法
function namespace(ns) { if (!ns) return; var nsArr = ns.split('.'); var root = window[nsArr[0]] || (window[nsArr[0]] = {}); for ( var i = 1; i < nsArr.length; i++) { root = root[nsArr[i]] || (root[nsArr[i]] = {}); } return root; } namespace("Edu.fn"); Edu.fn.add = function(a, b) { return a + b; } alert(Edu.fn.add(1, 2)); |
9. 编程常用的模板
针对一些工具类,或者全局只需要存在一个对象的写法
;var YourObj = null; (function($) { YourObj = { width:10, show:function(){} } })(jQuery); |
说明:前后加分号只是为了js代码合并压缩的时候保证不会影响别人的代码以及自己的代码。这里使用了js闭包的方式。把对象定义在外边的好处是eclipse的outline可以方便的显示对象的属性和方法。
类模板
; var YourClass = null; (function($) { YourClass = function(options) { this.settings = { width : 5, height : 5 };// 定义一些默认的属性 $.extend(true, this.settings, options); }; YourClass.prototype = { width : 10, name : '', show : function() { alert(this.name + ",width:" + width); } } })(jQuery); |
10. 常见的一些js用法
简化if语句
a?(语句):(语句)
a=arg||’0’;
11. js调试
现代浏览器一般都支持console.log(msg),要比alert方便,alert会阻断程序。
【参考资料】
http://www.w3school.com.cn/js/pro_js_object_defining.asp
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html
http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html
http://coolshell.cn/articles/6441.html
本文为作者原创,转载请注明出处,与你分享我的快乐
http://www.cnblogs.com/weirhp
Js面向对象编程的更多相关文章
- 带你一分钟理解闭包--js面向对象编程
上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...
- js原生设计模式——3简单工厂模式\js面向对象编程实例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- JS面向对象编程(进阶理解)
JS 面向对象编程 如何创建JS对象 JSON语法声明对象(直接量声明对象) var obj = {}; 使用 Object 创建对象 var obj = new Object(); JS对象可以后期 ...
- 页面循环绑定(变量污染问题),js面向对象编程(对象属性增删改查),js字符串操作,js数组操作
页面循环绑定(变量污染问题) var lis = document.querySelectorAll(".ul li") for ( var i = 0 ; i < lis. ...
- js面向对象编程 ---- 系列教程
原 js面向对象编程:数据的缓存 原 js面向对象编程:如何检测对象类型 原 js面向对象编程:if中可以使用那些作为判断条件呢? 原 js面向对象编程:this到底代表什么?第二篇 原 js面向对象 ...
- 简单粗暴地理解js原型链–js面向对象编程
简单粗暴地理解js原型链–js面向对象编程 作者:茄果 链接:http://www.cnblogs.com/qieguo/archive/2016/05/03/5451626.html 原型链理解起来 ...
- JS面向对象编程,对象,属性,方法。
document.write('<script type="text/javascript" src="http://api.map.baidu.com/api?v ...
- js面向对象编程(第2版)——js继承多种方式
附带书籍地址: js面向对象编程(第2版)
- 原生js面向对象编程-选项卡(自动轮播)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
随机推荐
- C#/.NET code
Some practices to write better C#/.NET code(译) C#(.NET)中有关编码的一些建议,原文地址:http://www.codeproject.com/Ar ...
- linux_awk_内部正则过滤
awk -F'\t' '{if($3 !~ /^<p><img/){print $0}}' latex500.db|wc -l
- 绘制一个绿色矩形平面((50, 50)->(350, 350))
//VS2008+opencv2.4 //绘制一个绿色矩形平面 #include "stdafx.h" #include "highgui.h" #includ ...
- timesten备份和恢复
ttIsql "DSN=ttwind;UID=cacheuser;PWD=cacheuser;OraclePWD=cacheuser;" --1.查看当前版本号 Command&g ...
- 使用Navicat Premium 和PL\SQL Developer连接Oracl
在64位Win7中使用Navicat Premium 和PL\SQL Developer连接Oracle数据库备忘 最近接手了一个项目,服务器端数据库是oracle 11g 64位.由于主要工作不 ...
- Reactive Extensions(Rx)并发浅析
Reactive Extensions(Rx)并发浅析 iSun Design & Code .Net并行编程 - Reactive Extensions(Rx)并发浅析 关于Reactive ...
- youcompleteme install and configure
三四年前我配置vim插件的时候还都是手动配置,还没有听说vundle这个vim插件管理工具,现在配置vim插件的时候基本上都用vundle,这个插件也确实是方便快捷,只不过这东西时能话了就不清楚里面的 ...
- 关于s2sh框架关于hibernate懒加载问题的说明和解决方案
首先我们来看下面这个图,解释了一个web程序的一次请求流程! 懒加载异常的说明: 懒加载就是我们在查询一个对象时,它所有的属性是否要被查出来! 如果懒加载开启的话,session在service层就被 ...
- loadrunner必用函数web_reg_save_param获取多个符合边界值条件的使用方法
在做loadrunner性能脚本开发时,常常碰见一个需求:符合web_reg_save_param函数中定义的左右边界值的值有多个,而我们的常规写法默认返回的是符合条件的第一个,而有时我们却需要使用后 ...
- 3. SQL Server数据库状态监控 - 可用空间
原文:3. SQL Server数据库状态监控 - 可用空间 数据库用来存放数据,那么肯定需要存储空间,所以对磁盘空间的监视自然就很有必要了. 一. 磁盘可用空间 1. 操作系统命令或脚本.接口或工具 ...