opp(Object Oriented Programming)

嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙。

好了,废话也不多说,把自己学到的放上来吧。嗯,说什么好呢,就说原型链啊

原型对象

  每个javascript对象都有一个原型对象,这个对象在不同的解释器下的实现不同。比如在firefox下,每个对象都有一个隐藏的__proto__属性,这个属性就是“原型对象”的引用。

原型链

  由于原型对象本身也是对象,根据上边的定义,它也有自己的原型,而它自己的原型对象又可以有自己的原型,这样就组成了一条链,这个就是原型链,JavaScritp引擎在访问对象的属性时,如果在对象本身中没有找到,则会去原型链中查找,如果找到,直接返回值,如果整个链都遍历且没有找到属性,则返回undefined.原型链一般实现为一个链表,这样就可以按照一定的顺序来查找。

怎么讲呢?直接把例子放上来讲好了

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
	<script type="text/javascript">
		window.onload = function() {                        /*这里就大概了解一下,每一个对象都有一个原型,原型对象的方法属性,对象都可以用*/
			function foo() {
				this.y = 2;
			}
			foo.prototype.x = 1;/*比如这里,定义了原型的属性,x=1*/
			var rr = new foo();
			console.log(rr.y + "和" + rr.x);/*实例化后,可以直接调用原型对象的属性,把原型看成一个类或者方法的集合就可以了*/

/*继承*/
			function person(name,sex){/*构造函数*/
				this.name=name;
				this.sex=sex;
			}
			person.bb="我是person 上的";
			person.prototype.hi=function(){
				console.log(this.name+"你好"+"我是"+this.sex+"的");
			}
			person.prototype.walk=function(){
				console.log(this.name+"正在走路");
			}
			person.prototype.a=2
			function student(name,sex,like){
				person.call(this,name,sex);/*我这里用call方法来继承*/
				this.like=like;
			}
			student.prototype=Object.create(person.prototype); /*这里用Object.create(继承谁的?)来继承prototype,实质是person.prototype的一个空对象赋值给student.prototype*/
			student.prototype.constructor = student;/*再把名字重新定义过来*/

			Object.prototype.ting = function(){
				console.log(this.name+"正在听")
			}
			student.prototype.chi = function(){
				console.log(this.name+"喜欢吃"+this.like);
			}
			student.prototype.pa = function(xiezi){
				console.log(this.name+"穿上"+xiezi+"奔跑");
			}
			var kk = new student("bob","男","冰淇淋")/*new 实例化对象*/
//			console.log(kk.a);
//			console.log(kk.hi());
     		        console.log(kk.ting())
//			console.log(typeof student.prototype);/*typeof 查看目标是什么类型的*/
			console.log(kk.pa("鞋子"));

			console.log(kk instanceof person.bb)/*intanceof查看右边有没有在左边的原型链上*/
			console.log(kk instanceof person)
			console.log(typeof kk)

		}
	</script>

   </body>
</html>

  

时间: 2024-08-26 19:08:33

opp(Object Oriented Programming)的相关文章

Java Object Oriented Programming concepts

Introduction This tutorial will help you to understand about Java OOP'S concepts with examples. Let's discuss about what are the features of Object Oriented Programming. Writing object-oriented programs involves creating classes, creating objects fro

Object Oriented Programming python

new concepts of the object oriented programming : class encapsulation inheritance polymorphism the three features of an object are : identity, state and behaviora class is an abstraction which regroup objects who have the same attributes and the same

CSE210 Advanced Object Oriented Programming

CSE210 Advanced Object Oriented ProgrammingCoursework 2019 Release date: 11th, Mar, 2019Deadline: 12:00PM, 23rd, Apr, 2019 1. DescriptionThe objective of the coursework is to develop a practical application for data processing, analysis and content s

MQF Object Oriented Programming

MQF Object Oriented Programming I Fall 2019Hw2 Due 10/1/2019 before midnightSpecificationsRutgers parking garage management system is required to take care Rutgers University Paringneeds. The system can keep track of all the cars parked in your garag

CSC72002 Object Oriented Programming

CSC72002 Object Oriented Programming - Assignment 2Weight: 40% of your final markSpecificationsYour task is to complete various exercises in NetBeans, using the Java language, and to submitthese via the MySCU link created for this purpose.Marking cri

what&#39;s the problem of Object oriented programming

The problem came from the Object itselft.It can divided into two aspect: 1.Everything is an Object: that's not true,lots of so called object is  not object ,it's just a Wrapper.You dont know what it is  ,but you just need one to let  things go on. Sa

面向对象编程Object Oriented Programming(OOP)

把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数通过切割成小块函数来降低系统的复杂度. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数通过切割成小块函数来降低系统的复杂度. 一:封装(类内的事) 假设我们要处理学生的成绩表,为了表示一个学生的成绩,面向过程的

[email&#160;protected] [355] Design Twitter (Object Oriented Programming)

https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the follow

object oriented programming : class application

class Thread_Sync; class Critical; class Info; class Info{Info(std::string str):m_info(str){} private: std::string m_info;}; // a process need: { pre-run();run();post-run();mutex for threads, running previlledge} #define APPL_DECLARE_APP( YourAppClas