/*
//工厂模式
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面向对象和原型的更多相关文章

  1. 第一百零九节,JavaScript面向对象与原型

    JavaScript面向对象与原型 学习要点: 1.学习条件 2.创建对象 3.原型 4.继承 ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标 ...

  2. 有关javaScript面向对象和原型笔记

    javaScript是一种比較特殊的语言,ECMAScript中没有类的概念.跟其它面向对象的语言有一定的差别.它的对象也与基于类的语言中的对象有所不同,严格来说,javascript对象是一组没有特 ...

  3. 了解JavaScript 面向对象基础 & 原型与对象

    面向对象语言中的对象 老是能听到什么基于对象, 面向对象. 什么是对象, 如果有面向对象基础的人可以无视了, 下面举个简单的例子给大家讲讲面向对象中, 对象的定义, 这个是比较通用的, 不过对于JS来 ...

  4. JavaScript面向对象和原型函数

    对象,是javascript中非常重要的一个梗,是否能透彻的理解它直接关系到你对整个javascript体系的基础理解,说白了,javascript就是一群对象在搅..(哔!). 常用的几种对象创建模 ...

  5. JavaScript面向对象与原型

    工厂模式:无法识别对象 function createObject(name, age) { //集中实例化的函数 var obj = new Object(); obj.name = name; o ...

  6. JavaScript 面向对象与原型

    ECMAScript有两种开发模式:1.函数式(过程化);2.面向对象(OOP); 一 创建对象1.普通的创建对象 ? 1 2 3 4 5 6 7 8 9 // 创建一个对象,然后给这个对象新的属性和 ...

  7. 快速理解JavaScript面向对象编程—原型

    总的来说js语言就是门面向对象编程的语言,对象这个概念几乎贯穿了整个js的学习. 对象 创建对象两种方法:(若要生成对象实例必须调用构造函数) 1.var obj = {name:"jer& ...

  8. JavaScript 面向对象之原型对象

    原型的概述 我们创建的每个函数都有一个 prototype(原型)属性,这个属性是一个对象,它的用途是包含可以由特定类型的所有实例共享的属性和方法. 逻辑上可以这么理解:prototype 通过调用构 ...

  9. JavaScript——面向对象与原型

    在最外面使用this,此时this是window作用域下的,因此他指向全局变量 对象冒充: 实例属性不会共享!

随机推荐

  1. autoit UIA获取Listview的信息

    #include "CUIAutomation2.au3" Opt( ) Global $oUIAutomation MainFunc() Func MainFunc() ; Be ...

  2. 学习总结 html图片热点,网页划区,拼接,表单

    表单: action="负责处理的 <form id="" name="" method="post/get"服务端&quo ...

  3. Android Malware Analysis

    A friend of mine asked me help him to examine his Android 5.0 smartphone. He did not say what's wron ...

  4. leetcode 121

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  5. PayPal 开发详解(一):注册PayPal帐号

    1.注册paypal帐号 https://www.paypal.com 2.使用刚才注册的paypal帐号登录3.进入开发者中心 4.登录开发者中心 5.登录 查看我们paypal Sandbox测试 ...

  6. CSS3 图片悬浮缩放效果

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. 2. XAML

    1. 什么是 XAML XAML 可以说是 XML 的一个特殊子集,使用相同的语法,只是 XML 可以自定义任何的节点和属性,但 XAML 是有所限制的,只能在规定的命名空间下使用. 2. names ...

  8. 个人博客实现Archives查询小记

    这两天正在做博客,刚刚遇到一个问题,就是需要在主页实现文档分类功能,即通过日期将文章进行按日期进行分类. 比如这样的: 我个人的想法是,查询所有文章的日期,然后将日期进行格式化,只留下年份和月份,然后 ...

  9. linux使用dd命令快速生成大文件

    dd命令可以轻易实现创建指定大小的文件,如 dd if=/dev/zero of=test bs=1M count=1000 会生成一个1000M的test文件,文件内容为全0(因从/dev/zero ...

  10. 软件工程 speedsnail 冲刺7

    2015-5-11 完成任务:蜗牛移动的一部分: 遇到问题: 问题1 速度,坐标,角度: 速度分级别设置: 坐标记录功能,方便障碍物检测: 暂定初始45度角: 解决1 未解决上述问题 明日任务: 蜗牛 ...