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. js中的new关键字都干了些什么?

    new 操作符 在有上面的基础概念的介绍之后,在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在javascript中,我们将这类方式成为Pseudoclassic ...

  2. python def说明

    可以这样讲,def定义了一个模块的变量,或者说是类的变量.它本身是一个函数对象.属于对象的函数,就是对象的属性.当然,你也可以叫它“方法”. python 的函数和其他语言的函数有很大区别.它是可以被 ...

  3. GCC编译源代码的四个步骤【转】

    GCC编译C源代码有四个步骤:预处理---->编译---->汇编---->链接. 可以利用GCC的参数来控制执行的过程,这样就可以更深入的了解编译C程序的过程. 下面将通过对一个程序 ...

  4. HW6.26

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  5. HDU-4639 Hehe 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4639 简单递推题,呵呵,不多说... //STATUS:C++_AC_15MS_272KB #incl ...

  6. Windows下Android SDK Manage下载速度缓慢的解决方法

    在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched using ...

  7. Spring配置文件的加载,及装载多个beans.xml文件

    applicationContext.xml 是spring的全局配置文件,用来控制srping的特性 1  手动加载自定义的beans.xml文件 @Test public void testAut ...

  8. soliworks三维机柜布局(三)绘制电气线路图

    三维机柜布局中的自动布线是根据线路图中的电气连接属性布的.

  9. nyoj 79 拦截导弹

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 某国为了防御敌国的导弹袭击,发展中一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到 ...

  10. svn访问权限控制

    [customer:/]qa = rwreadonly = ryinqixian = r@haowu_partner_dev = r@admin = rw[customer:/branches/v1. ...