javascript面向对象和原型
- /*
- //工厂模式
- function createObject(name,age){
- var obj = new Object();//新建一个对象
- obj.name=name;//新建对象的属性
- obj.age=age;
- obj.run=function(){//新建对象的方法
- return this.name+","+this.age+"...running.";
- }
- return obj;//返回新建的对象引用
- }
- var box1 = createObject("Carl",20);
- var box2 = createObject("Zhang",25);
- alert(box1.run());
- alert(box2.run());
- //构造函数
- function Box(name,age){
- this.name=name;//新建对象的属性
- this.age=age;
- this.run=function(){//新建对象的方法
- return this.name+","+this.age+"...running.";
- }
- }
- var box1 = new Box("Carl",20);//对象实例化
- var box2 = new Box("Zhang",25);
- alert(box1.run());
- alert(box2.run());
- alert(box1 instanceof Object);
- //构造函数
- function box(name,age){
- this.name=name;//新建对象的属性
- this.age=age;
- this.run=function(){//新建对象的方法
- return this.name+","+this.age+"...running.";
- }
- }
- var box1 = new box("Carl",20);//对象实例化
- var box2 = new box("Zhang",25);
- alert(box1.run());
- alert(box2.run());
- alert(box1 instanceof Object);
- //构造函数
- function Box(name,age){
- this.name=name;//新建对象的属性
- this.age=age;
- this.run=function(){//新建对象的方法
- return this.name+","+this.age+"...running.";
- }
- }
- var o = new Object();
- Box.call(o,"Carl",20);//call 对象冒充
- alert(o.run())
- //构造函数
- function Box(name,age){
- this.name=name;//新建对象的属性
- this.age=age;
- this.run=new Function("return this.name+this.age+'..running...'");
- }
- var o = new Object();
- Box.call(o,"Carl",20);//call 对象冒充
- alert(o.run());
- //构造函数
- function Box(name){
- this.name=name;//实例属性
- }
- Box.prototype.name="Zhang";//原型属性
- Box.prototype.age=25;//原型属性
- Box.prototype.run=function(){//原型方法
- return this.name+","+this.age+"...running.";
- }
- //构造函数体内的叫实例属性,每次新建的对象,其实例属性所在地址都是不一样的
- //prototype是一个引用,指向一个对象,通过prototype创建的属性,叫做原型属性,每次新建的对象的原型属性地址是一样的。
- var box1 = new Box("Carl");//对象实例化
- alert(box1.run());
- alert(box1.constructor);//构造函数,返回构造函数本身
- alert(box1.hasOwnProperty("name"));//判断实例属性中是否含有name属性
- delete box1.name;//删除实例属性
- alert("name" in box1); //true 判断是否存在于实例属性或者原型属性
- //构造函数
- function Box(name){
- this.name=name;//实例属性
- }
- Box.prototype={
- name:"Zhang",//原型属性
- age:25,//原型属性
- run:function(){//原型方法
- return this.name+","+this.age+"...running.";
- }
- }
- var box = new Box();
- alert(box.constructor);//Ojbect
- //构造函数
- function Box(name){
- this.name=name;//实例属性
- }
- Box.prototype={
- constructor:Box,
- name:"Zhang",//原型属性
- age:25,//原型属性
- run:function(){//原型方法
- return this.name+","+this.age+"...running.";
- }
- }
- var box = new Box();
- alert(box.constructor);//Box
- //构造函数
- function Box(name){
- this.name=name;//实例属性
- }
- Box.prototype={
- constructor:Box,
- name:"Zhang",//原型属性
- age:25,//原型属性
- run:function(){//原型方法
- return this.name+","+this.age+"...running.";
- }
- }
- //重写原型
- Box.prototyp={
- name:"Zhang"
- }
- var box = new Box();
- alert(box.run());//error
- var box=[2,1,29,3,0,13];
- alert(box.sort());
- alert(Array.prototype.sort);//查看sort是否为Array的原型函数
- alert(String.prototype.addstring);
- String.prototype.addstring=function(){
- return this+"...我是扩展的方法";
- }
- alert("Test addstring".addstring());
- //动态原型模式
- //构造函数
- function Box(name,age){
- this.name=name;//实例属性
- this.age=age;
- //将原型封装到构造函数中
- Box.prototype.run=function(){
- return this.name+this.age+"running...";
- }
- }
- var box1= new Box("Carl",10);
- var box2= new Box("Zhang",20);
- alert(box1.run());
- alert(box2.run());
- //构造函数
- function Box(name,age){
- this.name=name;//实例属性
- this.age=age;
- //将原型封装到构造函数中
- if(typeof this.run!='function'){//加上这段可以使得下面的原型只初始化一次
- alert("初始化开始");
- Box.prototype.run=function(){
- return this.name+this.age+"running...";
- }
- alert("初始化结束");
- }
- }
- var box1= new Box("Carl",10);
- var box2= new Box("Zhang",20);
- //继承,通过原型链实现
- function Box(){ //继承的函数叫做超类(父类,基类)
- this.name="Carl";
- }
- function Desk(){ //继承的函数叫做子类型(子类,派生类)
- this.age=100;
- }
- //通过原型链继承,超类型实例化后的对象实例,赋值给子类型的原型属性
- Desk.prototype=new Box();//new Box()会将Box构造里的信息和原型里的信息全都交给Desk
- var desk = new Desk();
- alert(desk.name);
- */
- /*
- //1.原型链继承 2.借用构造函数继承(对象冒充继承) 3.组合继承(结合前两种)
- //4.原型式继承
- //临时中转函数
- function obj(o){
- function F(){};
- F.prototype=o;
- return new F();
- }
- box={
- name:"Carl",
- age:20,
- family:["哥哥","姐姐","弟弟"]
- };
- var box1 = obj(box);
- alert(box1.family);
- box1.family.push("妹妹");
- alert(box1.family);
- var box2 = obj(box);
- alert(box2.family);
- */
- //临时中转函数
- function obj(o){
- function F(){};
- F.prototype=o;
- return new F();
- }
- //寄生函数
- function create(box,desk){
- var f=obj(box.prototype);
- f.constructor=desk;//调整原型构造指针
- desk.prototype=f;
- }
- function Box(name,age){
- this.name=name;
- this.age=age;
- }
- Box.prototype.run=function(){
- return this.name+this.age+" running...";
- }
- function Desk(name,age){
- Box.call(this,name,age); //对象冒充
- }
- //通过寄生组合继承来实现继承
- create(Box,Desk);//这句话用来代替Desk.prototype=new Box();
- var desk =new Desk("Carl",20);
- alert(desk.run());
- alert(desk.constructor);
- alert(desk.constructor.prototype);
- alert(desk.__proto__===desk.constructor.prototype);
备注:本文部分代码来自李炎辉老师的javascript教程
javascript面向对象和原型的更多相关文章
- 第一百零九节,JavaScript面向对象与原型
JavaScript面向对象与原型 学习要点: 1.学习条件 2.创建对象 3.原型 4.继承 ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标 ...
- 有关javaScript面向对象和原型笔记
javaScript是一种比較特殊的语言,ECMAScript中没有类的概念.跟其它面向对象的语言有一定的差别.它的对象也与基于类的语言中的对象有所不同,严格来说,javascript对象是一组没有特 ...
- 了解JavaScript 面向对象基础 & 原型与对象
面向对象语言中的对象 老是能听到什么基于对象, 面向对象. 什么是对象, 如果有面向对象基础的人可以无视了, 下面举个简单的例子给大家讲讲面向对象中, 对象的定义, 这个是比较通用的, 不过对于JS来 ...
- JavaScript面向对象和原型函数
对象,是javascript中非常重要的一个梗,是否能透彻的理解它直接关系到你对整个javascript体系的基础理解,说白了,javascript就是一群对象在搅..(哔!). 常用的几种对象创建模 ...
- JavaScript面向对象与原型
工厂模式:无法识别对象 function createObject(name, age) { //集中实例化的函数 var obj = new Object(); obj.name = name; o ...
- JavaScript 面向对象与原型
ECMAScript有两种开发模式:1.函数式(过程化);2.面向对象(OOP); 一 创建对象1.普通的创建对象 ? 1 2 3 4 5 6 7 8 9 // 创建一个对象,然后给这个对象新的属性和 ...
- 快速理解JavaScript面向对象编程—原型
总的来说js语言就是门面向对象编程的语言,对象这个概念几乎贯穿了整个js的学习. 对象 创建对象两种方法:(若要生成对象实例必须调用构造函数) 1.var obj = {name:"jer& ...
- JavaScript 面向对象之原型对象
原型的概述 我们创建的每个函数都有一个 prototype(原型)属性,这个属性是一个对象,它的用途是包含可以由特定类型的所有实例共享的属性和方法. 逻辑上可以这么理解:prototype 通过调用构 ...
- JavaScript——面向对象与原型
在最外面使用this,此时this是window作用域下的,因此他指向全局变量 对象冒充: 实例属性不会共享!
随机推荐
- COM学习(三)——数据类型
上回书介绍了GUID.CLSID.IID和接口的概念.本回的重点是介绍 COM 中的数据类型.咋还不介绍组件程序的设计步骤呀?咳......别着急,别着急!孔子曰:"饭要一口一口地吃&quo ...
- Windows phone 8 学习笔记(2) 数据文件操作(转)
Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方.本节主要讲解它们的用法以及相关限制性.另外包括本地数据库的使用方式 ...
- 【.NET】对文件的对称加密
using System;using System.IO;using System.Security.Cryptography;namespace ConsoleApp_SymmetricalEncr ...
- 1.3查看Linux内核版本
1.目前Linux内核主要维护的三个版本:Linux2.4.Linux2.6和Linux3.x,Android使用的是Linux2.6:Linux3.x是最新推出的Linux内核版本: 2.查看Lin ...
- javaSE第十三天
第十三天 76 1. StringBuffer(掌握) 76 (1)说明: 77 (2)StringBuffer的构造方法 77 (3)StringBuffer的常见功能 ...
- (一)、NodeJS (转载)
NodeJS基础 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...
- win2003以isapi的方式配置php+mysql环境(安装了shopEX)
一.准备相关组件 mysql-installer-community-5.5.29.0.zip php-5.2.17-Win32-VC6-x86 ZendOptimizer-3.3.3-Windows ...
- 刚开始学IOS遇到的类和方法
框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...
- sqoop的job工具
sqoop job: Work with saved jobs 就是将sqoop的某条语句保存为一个job 1.把person表导入到HDFS上,可以使用下面的语句 sqoop import --co ...
- mysql批量修改表引擎
生成修改的语句 SELECT CONCAT('ALTER TABLE ',table_name,' ENGINE=InnoDB;') FROM information_schema.tables WH ...