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 ...
随机推荐
- ASP.NET MVC学习之控制器篇扩展性
原文:ASP.NET MVC学习之控制器篇扩展性 一.前言 在之前的一篇随笔中已经讲述过控制器,而今天的随笔是作为之前的扩展. 二.正文 1.自定义动作方法 相信大家在开发过程一定会遇到动作方法的重名 ...
- openSUSE 安装
https://lug.ustc.edu.cn/sites/opensuse-guide/installation.php 开始 1. 简介2. 改用 GNU/Linux3. 获取 openSUSE4 ...
- ajax form表单提交 input file中的文件
ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了 ...
- C#中使用ref 和 out 的一点认识
ref 通常我们向方法中传递的是值,方法获得的是这些值的一个拷贝,然后使用这些拷贝,当方法运行完毕后,这些拷贝将被丢弃,而原来的值不会受到影响. 这种情况是通常的,当然还有另外一种情况,我们向方法传递 ...
- HDU 4052 Adding New Machine(矩形面积并)
Adding New Machine Problem Description Incredible Crazily Progressing Company (ICPC) suffered a lot ...
- Node.js与MongoDB的基本连接示例
Node.js与MongoDB的基本连接示例 前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务 ...
- ios MKNetworkKit 的使用
常用框架比如:AFNetworking,ASIHttpRequest,SDWebImage,MKNetWorKit等. iOS5已出来这么久了,而ASIHttpRequest的作者已经申明不更新了,在 ...
- NET Web开发
.NET Web开发初学者必知的四个网站 No.1 W3school 链接: http://www.w3school.com.cn/ 预览: 介绍: 全球最大Web前端技术教程网站.内容涵盖从基础 ...
- Windows环境搭建Web自动化测试框架Watir
Windows环境搭建Web自动化测试框架Watir 一.前言 Web自动化测试一直是一个比较迫切的问题,对于现在web开发的敏捷开发,却没有相对应的敏捷测试,故开此主题,一边研究,一边将We ...
- Singal Page App:使用Knockout和RequireJS创建高度模块化的单页应用引擎
Singal Page App 开篇扯淡 距离上一篇文章已经有好几个月,也不是没有时间记录点东西,主要是换了新的工作,在一家外资工作,目前的工作内容大多都是前端开发,新接触的东西因为时间原因,大多还不 ...