首页 > 编程语言 >JavaScript(03): 面向对象

JavaScript(03): 面向对象

时间:2023-06-20 13:04:44浏览次数:117  
标签:function 03 name 对象 JavaScript alert 面向对象 var new


面向对象的开发方式是目前软件开发的主流方式,JavaScript也是面向对象事件驱动的编程语言,如果能够掌握JavaScript的面向对象编程的知识,就有了构建自己的JavaScript库的基础和前提。

1. 面向对象的基本概念

对于熟悉Java或者C#的开发者来说,面向对象的概念以及封装、继承、多态等名词应该是不陌生的。ECMAScript并没有正式的类,对象定义存放在一个函数——构造函数中。在JavaScript中构造函数与普通函数相比并没有什么特殊之处。

在ECMAScript中,对象由特性(attribute)构成,特性可以是原始值,也可以是引用值,如果特性存放的是函数,可以将其视为对象的方法(method),其他的视为对象的属性(property)。

2. 对象的创建和使用

对象的创建和销毁都在JavaScript执行过程中发生。对象是用关键字new后跟要实例化的类名创建的。

var obj = new Object();
var str = new String();



ECMAScript定义了垃圾回收机制,意味着不用专门销毁对象来释放内存。当一个对象不被引用时,该对象就被废除了,而垃圾回收器的运行会导致废除的对象被销毁。将对象的引用设置为null可以废除该对象。对于那些旧的浏览器,由于没有完全的垃圾回收机制,所以可能出现对象不能被正确销毁的情况,废除对象和它的所有特性是确保内存使用正确的最好方法。

3. 对象的类型

  • 本地对象:独立于宿主环境的ECMAScript实现提供的对象。包括:Object、Function、Array、String、Boolean、Number、Date、RegExp、Error等。
  • Array类:
  • var x = new Array(); x[0] = "red"; x[1] = "blue"; x[2] = "green";
  • var y = new Array(20);
  • var z = new Array("red", "green", "blue");
  • 数组最多可以存放4294967295(2的32次方减1),可以满足大多数程序设计的需要。
  • 数组具有length属性、split()、slice()、concat()等方法。
  • ECMAScript的Array类一个非常有意思的地方是它提供的方法使数组的行为与其他数据类型的行为相似。例如,将Array对象当做栈(stack)来使用,通过push()和pop()方法进行存取操作。同时也可以通过shift()和unshift()方法将数组当做队列(queue)进行操作。
  • 数组提供了sort()方法可以对其进行排序操作,同时还提供了reverse()方法对数组进行倒转。
  • Date类:
  • 与Java中的Date类非常类似
  • 内置对象:由ECMAScript实现提供的独立于宿主环境的所有对象。
  • Global对象:Global对象是ECMAScript中最特别的对象,它不能被直接访问或调用,但是ECMAScript中不存在独立的函数,所有函数都必须是某个对象的方法。因此我们之前提到的isNaN()、isFinite()、parseInt()、parseFloat()等都是Global对象的方法。除此之外,encodeURI()和encodeURIComponent()方法用于编码传递给浏览器的URI(例如要将空格编码成%20);当然与此对应有decodeURI()和decodeURIComponent()方法。最后,也是最强大的一个方法eval(),该方法就像整个ECMAScript的解释程序,接受要执行的JavaScript字符串。这种功能非常强大,不过也非常危险,存在被代码注入的可能性。
  • Math对象:提供了数学函数(不解释)
  • 宿主对象:所有BOM和DOM对象都是宿主对象(依赖于浏览器环境的对象,因此不可避免的会牵扯到浏览器兼容性问题)


4. 定义类或对象

  • 工厂方式
function createCar() {
    var temp = new Object;
    temp.color = "red";
    temp.brand = "Benz";
    temp.drive = function() {
        alert(this.brand + " is running...");
    }
    return temp;
}

var car = createCar();
car.drive();

  • 构造器方式
function Car(brand, color) {
	if(this instanceof Car) {
		this.brand = brand;
		this.color = color;
		this.drive = function() {
			alert(this.brand + " is running...");
		};
	}
}

var car = new Car("Benz", "black");
car.drive();

  • 原型方式
function Car() {
}

Car.prototype.brand = "Benz";
Car.prototype.color = "black";
Car.prototype.drive = function() {
	alert(this.brand + " is running...");
}

var car = new Car();
car.drive();

5. 继承

1. 继承的概念

继承使得一个类可以重用另一个类的方法和属性,同时对已有的类的功能进行扩展。

2. 继承的方式


  • 对象冒充

function Person(name) {
	this.name = name;
	this.eat = function(place) {
		alert(this.name + " is eating at " + place);
	};
}

function Student(name, id) {
	this.newMethod = Person;
	this.newMethod(name);
	delete this.newMethod;

	this.id = id;
	this.study = function() {
		alert(this.name + " is studying...");
	};
}

var p1 = new Person("LUO Hao");
var p2 = new Student("LIAO Qiang", 1234);
p1.eat("Jinjiang Hotel");
p2.eat("1st Dinning Hall");
p2.study();


  • call()方法

function Person(name) {
	this.name = name;
	this.eat = function(place) {
		alert(this.name + " is eating at " + place);
	};
}

function Student(name, id) {
	Person.call(this, name);

	this.id = id;
	this.study = function() {
		alert(this.name + " is studying...");
	};
}

var p1 = new Person("LUO Hao");
var p2 = new Student("LIAO Qiang", 1234);
p1.eat("Jinjiang Hotel");
p2.eat("1st Dinning Hall");
p2.study();


  • apply()方法

function Person(name) {
	this.name = name;
	this.eat = function(place) {
		alert(this.name + " is eating at " + place);
	};
}

function Student(name, id) {
	Person.apply(this, arguments); // 2nd parameter is an array

	this.id = id;
	this.study = function() {
		alert(this.name + " is studying...");
	};
}

var p1 = new Person("LUO Hao");
var p2 = new Student("LIAO Qiang", 1234);
p1.eat("Jinjiang Hotel");
p2.eat("1st Dinning Hall");
p2.study();


  • 原型链

function Person(name) {
	this.name = name;
	this.eat = function(place) {
		alert(this.name + " is eating at " + place);
	};
}

function Student() {
	this.name = name;
	this.id = id;
	this.study = function() {
		alert(this.name + " is studying...");
	};
}

Student.prototype = new Person(this.name);
Student.prototype.constructor = Student;

var p1 = new Person("LUO Hao");
var p2 = new Student("LIAO Qiang", 1234);
p1.eat("Jinjiang Hotel");
p2.eat("1st Dinning Hall");
p2.study();


既然提到了继承,就再看看多态(polymorphism)的例子:



  • function ParentClass() {
    		this.foo = function() {
    			alert("This is common");
    		}
    
    		this.method = function() {
    			alert("parent-class method");
    		}
    	}
    
    	// just the same as static method of a class
    	SubClass1.goo = function() {
    		alert("gogo1");
    	}
    	SubClass2.goo = function() {
    		alert("gogo2");
    	}
    
    	function SubClass1() {
    		this.method = function() {
    			alert("sub-class1 method");
    		}
    	}
    
    	function SubClass2() {
    		this.method = function() {
    			alert("sub-class2 method");
    		}
    	}
    
    	SubClass1.prototype = new ParentClass();
    	SubClass1.prototype.constructor = SubClass1;
    
    	SubClass2.prototype = new ParentClass();
    	SubClass2.prototype.constructor = SubClass2;
     
    	var o1 = new SubClass1();
    	var o2 = new SubClass2();
    	var array = new Array(2);
    	array.push(o1);
    	array.push(o2);
    
    	for(var y in array) {
    		if(array[y] instanceof SubClass1) {
    			SubClass1.goo();	// like static method
    		}
    		else if(array[y] instanceof SubClass2) {
    			SubClass2.goo();
    		}
    		array[y].foo();
    		array[y].method();	//polymorphism method
    	}


标签:function,03,name,对象,JavaScript,alert,面向对象,var,new
From: https://blog.51cto.com/u_16166070/6521805

相关文章

  • JavaScript(07): 实例2---网页广告漂浮效果(面向对象版)
    在上一个版本的基础上使用JavaScript的面向对象完成,为了不影响阅读,去掉了随滚动条移动的广告<!DOCTYPEhtml><html> <head> <title>Example</title> <metahttp-equiv="content-type"content="text/html;charset=GBK"/> <linktype="......
  • JavaScript(07): 实例3---Google Eye
    下面的例子源于GoogleEye(如下图所示的效果),通过这个例子可以好好体会一下JavaScript的面向对象编程。<!DOCTYPEhtml><html> <head> <title>GoogleEye</title> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <st......
  • JavaScript的对象深度克隆
    Object.prototype.clone=function(){ varnewObj={}; for(variinthis){ if(typeof(this[i])=="object"||typeof(this[i])=="function"){ newObj[i]=this[i].clone(); } else{ newObj[i]=this[i]; } } r......
  • 20230313 java.util.LinkedList
    简介java.util.LinkedListLinkedList相对ArrayList要复杂一些,不是因为链表操作比数组操作复杂,而是LinkedList实现了更多接口LinkedList除了实现List接口外,还实现了Queue和Deque接口,也就意味着可以作为队列或双向队列使用对链表的学习非常有帮助感悟对于接口的认......
  • JavaScript学习 -- 对象的属性描述对象
    一、声明一个对象let对象={name:"公众号:编程有你",pwd:123456};二、输出对象属性的描述//console.log(Object.getOwnPropertyDescriptors(对象))获取多有属性console.log(Object.getOwnPropertyDescriptor(对象,'name'))获取指定的属性三、运行结果:writable:true, ......
  • JavaScript 指定格式化日期的方式
    JavaScript指定格式化日期的方式1、使用let和const的方式functionformatDate(date,format){constmap={"M":date.getMonth()+1,//月份"d":date.getDate(),//日"h":date.getHours(),//小时"m":date.getMinut......
  • 华大电子MCU CIU32M010、CIU32M030嵌入式闪存及中断和事件
    1.嵌入式闪存1.1模块介绍CIU32M010、CIU32M030集成了嵌入式FLASH控制模块,该模块控制FLASH的擦除、编程以及读取数据。上电时会从FLASH中读取相关数据进行校验以及初始化配置,保证芯片程序在正确且安全的情况下运行。1.2功能特点•支持高达64K主闪存空间的FLASH•......
  • 从零开始学Python第03课:Python语言中的变量
    对于想学习编程的新手来说,有两个问题可能是他们很想知道的,其一是“什么是(计算机)程序”,其二是“写(计算机)程序能做什么”。先说说我对这两个问题的理解:程序是数据和指令的有序集合,写程序就是用数据和指令控制计算机做我们想让它做的事情。今时今日,为什么有那么多人选择用Python语言......
  • Python开发系列课程(9) - 面向对象编程基础
    面向对象编程基础活在当下的程序员应该都听过“面向对象编程”一词,也经常有人问能不能用一句话解释下什么是“面向对象编程”,我们先来看看比较正式的说法。把一组数据结构和处理它们的方法组成对象(object),把相同行为的对象归纳为类(class),通过类的封装(encapsulation)隐藏内部细节,通过继......
  • 面向对象和面向过程的区别
    面向过程优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机、嵌入式开发、Linux/Unix等一般采用面向过程开发,性能是最重要的因素。缺点:没有面向对象易维护、易复用、易扩展。 面向对象优点:易维护、易复用、易扩展,由于面向对象有封装、继承、多......