`

JavaScript面向对象编程

阅读更多

实例一:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
<script type="text/javascript">
	function CreatePerson() {
	}
	CreatePerson.prototype.name="Tom";
	CreatePerson.prototype.sex="男";
	CreatePerson.prototype.showName = function() {
		alert("我的名字是叫" + this.name);
	};
	CreatePerson.prototype.showAge = function() {
		alert("我是" + this.sex + "的");
	};
	var person1 = new CreatePerson();
	person1.showName();	//我的名字是叫Tom

	var person2 = new CreatePerson();
	person2.showName();	//我的名字是叫Tom
	alert(person1.showName == person2.showName); //true;
	alert(person1 == person2);	//false
</script>
</html>

 

实例二:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>JavaScript面向对象编程,JavaScript中属性访问</div>
</body>
<script type="text/javascript">
	var book = {
		_year: 2004,
		edition: 1
	};
	Object.defineProperty(book, "year", {
		get: function() {
			return this._year;
		},
		set: function(newValue) {
			if(newValue > 2004) {
				this._year = newValue;
				this.edition += newValue - 2004;
			}
		}
	});
	book.year = 2005;
	alert(book.edition);//2
</script>
</html>

 

实例三:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>JavaScript面向对象编程,工厂模式</div>
</body>
<script type="text/javascript">
	function createPerson(name,sex) {	//这个函数是用来创建一个对象的,就叫构造函数
		var person = new Object();
		person.name=name;
		person.sex=sex;
		person.showName=function() {
			alert("我的名字是叫" + this.name);
		};
		person.showAge = function() {
			alert("我是" + this.sex + "的");
		};
		return person;
	}
	var person1 = createPerson("Tom","男");
	var person2 = createPerson("Hanmeimei","女");
	person1.showName();	//我的名字是叫Tom
	person1.showAge();	//我是男的
	person2.showName();	//我的名字是叫Hanmeimei
	person2.showAge();	//我是女的
</script>
</html>

 

实例四:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>JavaScript面向对象编程,解决没有new的问题</div>
</body>
<script type="text/javascript">
	function CreatePerson(name,sex) {
		//假想的系统内部工作流程
		//var this = new Object();
		this.name = name;
		this.sex = sex;
		this.showName = function() {
			alert("我的名字是叫" + this.name);
		};
		this.showAge = function() {
			alert("我是" + this.sex + "的");
		};
		//假想的系统内部工作流程
		//return this;
	}
	var person1 = new CreatePerson("Tom","男");
	var person2 = new CreatePerson("Hanmeimei","女");
	person1.showName();	//我的名字是叫Tom
	person1.showAge();	//我是男的
	person2.showName();	//我的名字是叫Hanmeimei
	person2.showAge();	//我是女的
</script>
</html>
分享到:
评论

相关推荐

    JavaScript面向对象编程指南.pdf

    JavaScript作为一门浏览器语言的核心思想;面向对象编程的基础知识及其在... 《JavaScript面向对象编程指南》着重介绍JavaScript在面向对象方面的特性,展示如何构建强健的、可维护的、功能强大的应用程序及程序库

    JavaScript面向对象编程指南

    《Javascript面向对象编程指南》着重介绍Javascript在面向对象方面的特性,展示如何构建强健的、可维护的、功能强大的应用程序及程序库。 全书包括8章和3个附录,依次介绍了Javascript的发展历史...

    JavaScript面向对象编程指南(第2版)

    资源名称:JavaScript面向对象编程指南(第2版)内容简介:1.本书是唯一一本介绍Javascript面向对象编程的图书。2.本书作者是知名的Web开发人员和作者。受到国内众多前端开发人员,如淘宝UED团队的推崇和推荐...

    JavaScript面向对象编程指南完整版

    JavaScript面向对象编程指南是完整的扫描版...

    JavaScript面向对象程序设计

    JavaScript面向对象程序设计(1): 前言 JavaScript面向对象程序设计(2): 数组 JavaScript面向对象程序设计(3): 对象 JavaScript面向对象程序设计(4): 函数 JavaScript面向对象程序设计(5): 类 JavaScript面向对象程序...

    javascript面向对象编程指南 2nd

    javascript面向对象编程指南 2nd英文版,英文名:Object-Oriented JavaScript。 What you will learn from this book The basics of object-oriented programming, and how to apply it in the JavaScript ...

    Javascript面向对象编程.

    NULL 博文链接:https://goyourauntie.iteye.com/blog/1179204

    javascript面向对象编程

    资源名称:Javascript面向对象编程   内容简介: 从语言的视角来看,面向对象的程序设计和面向对象的Javascript 语言绝对不是什么摩登的  东西;Javascript 最开始就是被设计成一...

    JavaScript面向对象编程指南 pdf

    JavaScript面向对象编程指南 pdf,学习JavaScript实用,难得。

    JavaScript面向对象编程.pdf

    JavaScript面向对象编程.pdf

    JavaScript面向对象编程指南 完整版

    JavaScript面向对象编程指南完整版是扫描的....

Global site tag (gtag.js) - Google Analytics