1、javascript深度克隆:

//注意这里的对象包括object和array

function cloneObject(obj){
  var o = obj.constructor === Array ? [] : {};
  for(var key in obj){
    if(obj.hasOwnProperty(key)){
      o[key] = typeof obj[key] === "object" ? cloneObject(obj[key]) : obj[key];
    }
  }
  return o;
}

Object.prototype.cloneObject=function(){

  var o=this.constructor===Array?[]:{};

  for(var key in this){

    if(this.hasOwnProperty(key)){
      o[key] = typeof this[key] === "object" ? cloneObject(this[key]) : this[key];
    }
  }
  return o;
}

//这个方法只能用于被拷贝的对象中元素中没有引用类型的值。

Object.prototype.cloneObject=function(){

  var str=JSON.stringify(this);

  return JSON.parse(str);

}

2、javascript的继承实现:

第一种 prototype 引用型原型继承

<script>
function parent(){
    this.x=10;
}
function child(){
}
child.prototype=new parent();
var childObj=new child();
alert(childObj.x);
</script>

第二种 类继承 属性抄写

<script>
function parent(){
    this.x=10;
}
function child(){
    var parentObj=new parent();
    for(var p in parentObj)this[p]=parentObj[p];
}
var childObj=new child();
alert(childObj.x);</script>

第三种 类继承 对象冒充

<script>
function parent(){
    this.x=10;
}
function child(){
    parent.call(this);
}
var childObj=new child();
alert(childObj.x);

</script>

第四种 原型抄写

<script>
function parent(){}
parent.prototype.me=function(){alert("parent")};
function child(){}
for(var p in parent.prototype){

  child.prototype[p]=parent.prototype[p];

}
var childObj=new child();
childObj.me();

</script>

第五种 混合方式
  混合了call方式、原型链方式

<script>
  function Parent(hello){

    this.hello = hello;

  }

  Parent.prototype.sayHello = function(){

    alert(this.hello);

  }

  function Child(world){

    Parent.call(this);//将父类的属性继承过来

    this.world = world;//新增一些属性

  }

Child.prototype = new Parent();//将父类的方法继承过来

Child.prototype.sayWorld = function(){//新增一些方法

    alert(this.world);

  }

var c = new Child("zhangsan","lisi");

  c.sayHello();

  c.sayWorld();

</script>

javascript深度克隆与javascript的继承实现的更多相关文章

  1. javascript深度克隆函数deepClone

    javascript深度克隆函数deepClone function deepClone(obj) { var _toString = Object.prototype.toString; // nu ...

  2. Javascript深度克隆一个对象

    Javascript中的对像赋值与Java中是一样的,都为引用传递.就是说,在把一个对像赋值给一个变量时,那么这个变量所指向的仍就是原来对 像的地址.那怎么来做呢?答案是“克隆”. 克隆有两种方法:一 ...

  3. 结构-行为-样式-Javascript 深度克隆函数(转)

    突然想到有一回面试的时候有一个问题一直挂在心头,于是乎在网上找了找,这个比较好: //深度克隆 function deepClone(obj) { var result, oClass = isCla ...

  4. JavaScript深度克隆(递归)

    今天在深度理解JQuery源码时,剖析extend时: jQuery.extend = jQuery.fn.extend = function() { //... } 感觉该方法的一部分功能与深度克隆 ...

  5. javascript 深度克隆对象

    js一般有两种不同数据类型的值: 基本类型(包括undefined,Null,boolean,String,Number),按值传递: 引用类型(包括数组,对象),按址传递,引用类型在值传递的时候是内 ...

  6. JavaScript深度克隆

    深度克隆函数: function deepClone(obj){ var str = ""; var newobj = obj.constructor === Array ? [] ...

  7. javascript深度克隆对象

    /** * * @param obj * @returns {*} */ //深度克隆 function cloneObject(obj) { if (obj === null || typeof(o ...

  8. javascript 深度克隆

    关键词 :递归 主要分为 数组 .对象.以及基本类型 function clone(Obj) {           var buf;           if (Obj instanceof Arr ...

  9. javascript中对象的深度克隆

    记录一个常见的面试题,javascript中对象的深度克隆,转载自:http://www.2cto.com/kf/201409/332955.html 今天就聊一下一个常见的笔试.面试题,js中对象的 ...

随机推荐

  1. Ajax异步请求-简单模版

    <script type="text/javascript"> window.onload = function () { document.getElementByI ...

  2. 解决JSP页面无法使用EasyUI里面class="easyui-dialog"的问题

    当使用MyEclipse新建一个JSP页面的时候,MyEclipse会自动添加一些标记,这些标记也许不一定会在工程中使用到.比如<base href="<%=basePath%& ...

  3. Redhat Enterprise Linux 6.4图形界面的中文问题

    一.界面中文,但Windows中的中文文件名上传到linux后乱码. .bashrc文件: export LANG=zh_CN.UTF-8 /etc/sysconfig/i18n文件: LANG=&q ...

  4. DOS命令关闭进程

    1.开始-运行,输入cmd后回车; 2.在DOS提示符下,先用命令 tasklist 回车来获取进程的 PID(例如获取了Explorer.EXE进程的PID为1988); 3.然后再输入命令:tas ...

  5. SGU131 - Hardwood floor(状态压缩DP)

    题目大意 给定一个N*M大小的矩形,要求你用1*2和2*2(缺个角)的砖块把矩形铺满(不能重叠),问总共有多少种铺法? 题解 受POJ2411的影响,怎么都没想到3,4,5,6这几种情况该怎么放置,看 ...

  6. hdoj 2097 Sky数

    Sky数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. iOS block的用法

    本章学习目标: 1. 了解何谓block. 2. 了解block的使用方法. Block 是iOS在4.0之后新增的程式语法,严格来说block的概念并不算是基础程式设计的范围,对初学者来说也不是很容 ...

  8. CentOS6.5安装nginx及负载均衡配置

    所有的安装包可以去以下地址下载,或者自行去官网下载,下面都有介绍. 所有安装包地址:http://download.csdn.net/detail/carboncomputer/9238037 原文地 ...

  9. 从Web借鉴UI设计

    从Web借鉴UI设计 用户体验已经成为衡量应用软件质量的重要标准.在过去我们可能会惊叹于某个Web应用的华丽界面,现在,随着HTML5的强势登场,各类表现层技术及开发框架的发布,Web与窗体应用的界限 ...

  10. eclipse 护眼色

    eclipse windows 窗口背景颜色 保护视力 博客分类: 工具使用 EclipseWindows  eclipse  窗口背景颜色 windows 窗口背景颜色