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. 自问自答之VR遐想

    先让我组织一下语言,作为表达能力超弱的战五渣来讲,归纳总结什么的最要命了. 我可以给你分析个1到N条出来,但是一般来讲没什么顺序,想到什么就说什么.而且我属于线性思维,有一个引子就可以按着话头一步步发 ...

  2. LeetCode题解——Longest Palindromic Substring

    题目: 给定一个字符串S,返回S中最长的回文子串.S最长为1000,且最长回文子串是唯一. 解法: ①遍历,对于每个字符,计算以它为中心的回文子串长度(长度为奇数),同时计算以它和右边相邻字符为中心的 ...

  3. 编译驱动时出现"Cannot open file trace.h"错误

    编译驱动时出现"Cannot open file trace.h"错误 如题,用VS2013编译驱动是出现上述错误,原来是开启了WPP追踪导致的: 解决方案: 右键项目名-属性-W ...

  4. 1N系列稳压二极管参数

    1N系列稳压二极管参数 型号 稳定电压 型号 稳定电压 型号 稳定电压 1N5236 7.5 1N5738 12 1N6002 12 1N5237 8.2 1N5739 13 1N6003 13 1N ...

  5. 转载 ASP.NET MVC中使用ASP.NET Identity

    转载原地址: http://blog.jobbole.com/90695/ 在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identit ...

  6. Java的面向对象思想

    多态性: 一种方法有多种实现,采用哪一种实现由Java虚拟机在运行时动态决定,这种能力成为动态绑定(dynamic binding),也称为多态性(polymorphism)(源于一个希腊单词,意为“ ...

  7. charindex的用法

    declare @str nvarchar(50);set @str='462,464,2';select @str as '字符串'select len(@str) as '字符长度' select ...

  8. 流弊博客集锦(updating)

    1.http://ifeve.com/ 2.淘宝的 code project http://code.taobao.org/ http://blog.csdn.net/tenfyguo/article ...

  9. 搭建maven+spring+mybatis工程

    一.maven 命令搭建web项目 可以参考我之前的一篇博文maven快速入门 1.搭建web工程 mvn archetype:generate -DgroupId=com.yuanmeng.spri ...

  10. c#后台修改前台DOM的css属性

    <div id = 'div1' runat="server">haha</div> ----------- 后台代码中这样调用 div1.Style[&q ...