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(truethis.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面向对象编程的更多相关文章

  1. 带你一分钟理解闭包--js面向对象编程

    上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...

  2. js原生设计模式——3简单工厂模式\js面向对象编程实例

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. JS面向对象编程(进阶理解)

    JS 面向对象编程 如何创建JS对象 JSON语法声明对象(直接量声明对象) var obj = {}; 使用 Object 创建对象 var obj = new Object(); JS对象可以后期 ...

  4. 页面循环绑定(变量污染问题),js面向对象编程(对象属性增删改查),js字符串操作,js数组操作

    页面循环绑定(变量污染问题) var lis = document.querySelectorAll(".ul li") for ( var i = 0 ; i < lis. ...

  5. js面向对象编程 ---- 系列教程

    原 js面向对象编程:数据的缓存 原 js面向对象编程:如何检测对象类型 原 js面向对象编程:if中可以使用那些作为判断条件呢? 原 js面向对象编程:this到底代表什么?第二篇 原 js面向对象 ...

  6. 简单粗暴地理解js原型链–js面向对象编程

    简单粗暴地理解js原型链–js面向对象编程 作者:茄果 链接:http://www.cnblogs.com/qieguo/archive/2016/05/03/5451626.html 原型链理解起来 ...

  7. JS面向对象编程,对象,属性,方法。

    document.write('<script type="text/javascript" src="http://api.map.baidu.com/api?v ...

  8. js面向对象编程(第2版)——js继承多种方式

    附带书籍地址: js面向对象编程(第2版)

  9. 原生js面向对象编程-选项卡(自动轮播)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

随机推荐

  1. Unity3D专访——真正的面试

    本来想写一系列的,一半的攻击,现在面试的水.人之奸,用大哥的话说,要走新手是做螺丝钉和抹布用的.还有一半是对出出学校的或者是自废武功转3d的朋友们提供一个比較有价值的參考. 只是我时间实在仓促.没有保 ...

  2. Cgroup maintainer丽泽范:解剖Linux核心容器技术

    摘要:Cgroup和namespace等内核特性如何出现,在社区处于如何的开发状况?Docker如火如荼.内核社区是否会因此加紧完好容器技术的隔离性安全性?华为Linux内核高级project师李泽帆 ...

  3. JS中5秒中跳转到其他页面

    原文:JS中5秒中跳转到其他页面 <head> <meta http-equiv="Content-Type" content="text/html; ...

  4. android studio 添加到项目库中的项目

    工程-对-new module-Android Library-module name和package name 它应该在同一个库被引入. 然后在该文件夹replace新的library 在gradl ...

  5. 高性能双端js模板---simplite

    simplite是一款js实现的模板引擎,它能够完成浏览器端js模版和node服务器端js模板的数据渲染,渲染性能达到引擎的极限. 渲染性能十分突出. 支持浏览器端和node服务器端模板渲染. 它简单 ...

  6. Linux进程和线程的比較

    进程与线程 參考:http://www.cnblogs.com/blueclue/archive/2010/07/16/1778855.html 首先比較Linux进程和线程的创建的差别,以此展开: ...

  7. 一步一步写算法(之prim算法 下)

    原文:一步一步写算法(之prim算法 下) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 前两篇博客我们讨论了prim最小生成树的算法,熟悉 ...

  8. 【剑指offer】二叉搜索树转双向链表

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26623795 题目描写叙述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表. ...

  9. Spring之SpringMVC(源码)启动初始化过程分析

    1.说明 SpringMVC作为Spring提供的MVC实现,可以实现与Spring的天然无缝联合,因为具有很广泛的用途.具体的关于SpringMVC的处理流程逻辑我在这里就不在赘述了.还是来通过源码 ...

  10. [译]Java内存泄露介绍

    (本文章翻译自the-introduction-of-memory-leak-what-why-and-how) Java最大的优势之一就是它的内存管理机制.你可以简单创建对象然后垃圾回收器会负责分配 ...