Javascript的难点就是面向对象编程,上一篇介绍了Javascript的两种继承方式:Javascript 进阶 继承,这篇使用一个例子来展示js如何面向对象编程,以及如何基于类实现继承。

1、利用面向对象的写法,实现下面这个功能,实时更新数据的一个例子:

2、使用对上面类的继承,完成下面的效果:

好了,不多说,js的训练全靠敲,所以如果觉得面向对象不是很扎实,可以照着敲一个,如果觉得很扎实了,提供了效果图,可以自己写试试。

1、第一个效果图代码:

/**
* Created with JetBrains WebStorm.
* User: zhy
* Date: 14-6-7
* Time: 下午4:55
* To change this template use File | Settings | File Templates.
*/
/**
* @param id
* @param value
* @param parentEle 父元素
* @constructor
*/
function PlaceFieldEditor(id, value, parentEle)
{
this.id = id;
this.value = value;
this.parentEle = parentEle;
this.initValue = value ; this.initElements();
this.initEvents();
} PlaceFieldEditor.prototype = {
constructor: PlaceFieldEditor,
/**
* 初始化所有元素
*/
initElements: function ()
{
this.txtEle = $("<span/>");
this.txtEle.text(this.value); this.textEle = $("<input type='text' />");
this.textEle.val(this.value); this.btnWapper = $("<div style='display: inline;'/>");
this.saveBtn = $("<input type='button' value='保存'/>");
this.cancelBtn = $("<input type='button' value='取消'/>");
this.btnWapper.append(this.saveBtn).append(this.cancelBtn); this.parentEle.append(this.txtEle).append(this.textEle).append(this.btnWapper); this.convertToReadable();
},
/**
* 初始化所有事件
*/
initEvents: function ()
{
var that = this;
this.txtEle.on("click", function (event)
{
that.convertToEditable();
}); this.cancelBtn.on("click", function (event)
{
that.cancel();
}); this.saveBtn.on("click", function (event)
{
that.save();
}); },
/**
* 切换到编辑模式
*/
convertToEditable: function ()
{
this.txtEle.hide();
this.textEle.show();
this.textEle.focus(); if(this.getValue() == this.initValue )
{
this.textEle.val("");
} this.btnWapper.show();
},
/**
* 点击保存
*/
save: function ()
{
this.setValue(this.textEle.val());
this.txtEle.html(this.getValue().replace(/\n/g,"<br/>")); var url = "id=" + this.id + "&value=" + this.value;
// alert(url);
console.log(url);
this.convertToReadable();
},
/**
* 点击取消
*/
cancel: function ()
{
this.textEle.val(this.getValue());
this.convertToReadable();
},
/**
* 切换到查看模式
*/
convertToReadable: function ()
{
this.txtEle.show();
this.textEle.hide();
this.btnWapper.hide();
},
setValue: function (value)
{
this.value = value;
},
getValue: function ()
{
return this.value;
}
}
;

引入到页面代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="PlaceFieldEditor.js"></script> <script type="text/javascript">
$(function ()
{ $("ul li").each(function ()
{
new PlaceFieldEditor($(this).attr("id"), "请输出成绩...", $(this));
}); }); </script> <style type="text/css">
body
{
font-size: 12px;
color: #333;;
} ul li
{
line-height: 30px;
} </style>
</head>
<body> <ul>
<li id="1">张三:</li>
<li id="2">李四:</li>
<li id="3">王二:</li>
</ul> </body>
</html>

嗯,代码就不详细说了,都比较简单,使用了jQuery,如果不喜欢可以使用原生js,本人比较喜欢把jQuery当作js的工具使用。

2、第二个效果图的js代码:

/**
* Created with JetBrains WebStorm.
* User: zhy
* Date: 14-6-7
* Time: 下午5:34
* To change this template use File | Settings | File Templates.
*/
function PlaceAreaEditor(id, value, parentEle)
{
PlaceAreaEditor.superClass.constructor.call(this, id, value, parentEle);
} extend(PlaceAreaEditor, PlaceFieldEditor); PlaceAreaEditor.prototype.initElements = function ()
{
this.txtEle = $("<span/>");
this.txtEle.text(this.value); this.textEle = $("<textarea style='width:315px;height:70px;' />");
this.textEle.text(this.value); this.btnWapper = $("<div style='display: block;'/>");
this.saveBtn = $("<input type='button' value='保存'/>");
this.cancelBtn = $("<input type='button' value='取消'/>");
this.btnWapper.append(this.saveBtn).append(this.cancelBtn); this.parentEle.append(this.txtEle).append(this.textEle).append(this.btnWapper); this.convertToReadable(); };

写了PlaceAreaEditor继承了PlaceFieldEditor,然后复写了initElements方法,改变了text为textarea。

extend的方法,上一篇博客已经介绍过:

/**
* @param subClass 子类
* @param superClass 父类
*/
function extend(subClass, superClass)
{
var F = function ()
{
};
F.prototype = superClass.prototype;
//子类的prototype指向F的_proto_ , _proto_又指向父类的prototype
subClass.prototype = new F();
//在子类上存储一个指向父类的prototype的属性,便于子类的构造方法中与父类的名称解耦 使用subClass.superClass.constructor.call代替superClass.call
subClass.superClass = superClass.prototype;
}

最后页面代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="PlaceFieldEditor.js"></script>
<script type="text/javascript" src="com.zhy.extend.utils.js"></script>
<script type="text/javascript" src="PlaceAreaEditor.js"></script> <script type="text/javascript"> $(function ()
{
$("ul li div").each(function ()
{
new PlaceAreaEditor($(this).attr("id"), "请留言...", $(this));
});
}); </script> <style type="text/css"> body
{
font-size: 12px;
color: #333;;
} ul li
{
padding: 5px 0 8px 0 ;
} </style>
</head>
<body> <ul>
<li id="1"><h3>我要改剧本,不让~~</h3>
<div>
</div>
</li> <li id="2"><h3>悬崖上有桥么,有?没有~ </h3>
<div>
</div>
</li>
<li id="3"><h3>你敢打坏我的灯?不租~ </h3>
<div>
</div>
</li>
</ul> </body>
</html>

好了,结束~~ 上面的例子是根据孔浩老师的例子修改的,感谢孔浩老师,孔老师地址:www.konghao.org。孔老师录制了很多Java相关视频,有兴趣的可以去他网站学习!

代码或者讲解有任何问题,欢迎留言指出。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Javascript 进阶 面向对象编程 继承的一个例子的更多相关文章

  1. Javascript 进阶 面向对象编程 继承的一个样例

    Javascript的难点就是面向对象编程,上一篇介绍了Javascript的两种继承方式:Javascript 进阶 继承.这篇使用一个样例来展示js怎样面向对象编程.以及怎样基于类实现继承. 1. ...

  2. JavaScript的面向对象编程(OOP)(一)——类

    在学习JavaScript面向对象的编程之前,需要知道,并了解面向对象的一些基本的常识.初学者中大多数都以为面向对象中,面向对象的编程是很重要和占据很大一部分精力.笔者在之前也是认为OOP是面向对象的 ...

  3. C++ Primer 学习笔记_69_面向对象编程 --继承情况下的类作用域

    面向对象编程 --继承情况下的类作用域 引言: 在继承情况下,派生类的作用域嵌套在基类作用域中:假设不能在派生类作用域中确定名字,就在外围基类作用域中查找该名字的定义. 正是这样的类作用域的层次嵌套使 ...

  4. Python 面向对象编程 继承 和多态

    Python 面向对象编程 继承 和多态 一:多继承性 对于java我们熟悉的是一个类只能继承一个父类:但是对于C++ 一个子类可以有多个父亲,同样对于 Python一个类也可以有多个父亲 格式: c ...

  5. Python面向对象编程——继承与派生

    Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ...

  6. 【前端学习】javascript面向对象编程(继承和复用)

    前言       继承,代码复用的一种模式.和其它高级程序语言相比,javascript有点点不一样,它是一门纯面向对象的语言,在JS中,没有类的概念,但也可以通过原型(prototype)来模拟对象 ...

  7. Javascript 面向对象编程—继承和封装

      前  言 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类) ...

  8. javascript进阶——面向对象特性

    面向对象的javascript是这门语言被设计出来时就考虑的问题,熟悉OOP编程的概念后,学习不同的语言都会发现不同语言的实现是不同的,javascript的面向对象特性与其他具有面向对象特性的语言的 ...

  9. javascript的面向对象编程

    面象对象编程技术的核心理念:封装.继承.多态:在一些主流的高级编程语言中,比如:C#,VB.NET,JAVA,PHP等都是很容易实现的,而如果要在javascript中实现面象对象编程,可就不那么直接 ...

随机推荐

  1. 安卓笔记--Edittext禁止换行

    直接在EditText的xml文件中通过配置android:singleLine="true"把虚拟键盘上的enter键禁止掉,不会显示 <edittext android: ...

  2. 速度之王 — LZ4压缩算法(一)

    LZ4 (Extremely Fast Compression algorithm) 项目:http://code.google.com/p/lz4/ 作者:Yann Collet 本文作者:zhan ...

  3. LeetCode之旅(21)-Swap Nodes in Pairs

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  4. 知物由学|游戏开发者如何从容应对Unity手游风险?

    本文由  网易云发布. "知物由学"是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不 ...

  5. Supervisor安装、配置、开启启动

    1.安装Python包管理工具(easy_install) wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - ...

  6. 锋利的Jquery摘要

    一本好书值得去反复回味 第一章: jquery中的$(document).ready(function(){})与js中的windows.onload()的比较   $(document).ready ...

  7. CRM客户关系管理系统(十一)

    第十一章.学员报名流程开发 11.1.面包屑的制作 Boorstrap路径导航条 (1)table_obj_list.html页面面包屑 def table_obj_list 返回数据改成locals ...

  8. Eclipse 3.5 以后安装插件很慢的解决办法

    1 .除非你需要,否则不要选择"联接到所有更新站点" 在安装对话框里有一个小复选框,其标示为"在安装过程中联接到所有更新站点从而找到所需的软件."从表面上看,这 ...

  9. Linux 库函数与系统调用的关系与区别

    上周总结了<C 标准库的基础 IO>,其实这些功能函数通过「系统调用」也能实现相应功能.这次文章并不是要详细介绍各系统调用接口的使用方法,而是要深入理解「库函数」与「系统」调用之间的关系和 ...

  10. JavaScript中对象数组,如何给对象添加一个新属性

    var a =[{name: 'Tom',age:20},{name: 'Tom2',age:22}] 现在给a数组中的第一个对象添加性别属性 a[0]['gender']='women' a[0][ ...