zoukankan      html  css  js  c++  java
  • 【前端学习笔记03】JavaScript对象相关方法及封装

    	//Object.create()创建对象
    	var obj = Object.create({aa:1,bb:2,cc:'c'});
    	obj.dd = 4;
    	console.log(obj.cc); // c
    	console.log(obj.hasOwnProperty("cc")); //false
    	console.log(obj.hasOwnProperty("dd")); // true
    
       //仿Object.create()方法创建CreateObject函数
    	var obj = {aa:1,bb:2,cc:'c'};
    	function CreateObject(proto){
    		function F(){};
    		F.prototype = proto;
    		return (new F);
    	}
    	var newObj = CreateObj(obj);
    
        //构造函数和原型结合创建对象举例
    	function Circle(r){
            this.r = r;
    	}
    	Circle.prototype.circum = function(){
    	    return (2*this.r*Math.PI).toFixed(2);
    	}
    	Circle.prototype.area = function(){
    	    return (Math.pow(this.r,2)*Math.PI).toFixed(2);
    	}
    	 
    	var c = new Circle(3);
    	console.log(c.circum());
    	console.log(c.area());
    
    	//复制一个对象 
    	function copyObj(obj){
    		var newObj = {};
    		for(key in obj){
    			newObj[key] = obj[key];
    		}
    		return newObj;
    	}
    
    	//类继承举例
    		//创建父类
    		function Father(){
    		    this.familyName = 'Zhu';
    		    this.money = 20;
    		}
    		//父类原型
    		Father.prototype.have= function(){
    		    return 'I have $'+this.money+'.'
    		}
    		//创建子类
    		function Child(){
    		    Father.apply(this,arguments);
    		    this.toy = 'car';
    		}
    		//子类原型等于父类构造的新对象
    		Child.prototype = new Father();
    		//子类的构造函数重定向
    		Child.constructor = Child;
    		 //子类可创建自己的方法
    		Child.prototype.play = function(){
    		    return 'I play with a '+this.toy+'.'
    		}
    		//基于子类创建新对象
    		var child = new Child();
    
    
    	//extend方法 对象扩展
    	function extend(target,source){
    		for(k in source){
    			target[k] = source[k];
    		}
    		return target;
    	}
  • 相关阅读:
    python解压缩rar,zip文件的正确姿势
    tensorflow1.x及tensorflow2.x不同版本实现验证码识别
    qt5.6.3下使用firebird
    Python3.6下的Requests登录及利用Cookies登录
    c++实现全密码生成
    android中利用HttpURLConnection进行Get、Post和Session读取页面。
    Freebsd10.3 Nginx多版本PHP
    Freebsd10.3(FreeBSD11 Beta1)使用手记
    Eclipse导入的User Libarary
    MySQL zip版本安装
  • 原文地址:https://www.cnblogs.com/zachary93/p/6054470.html
Copyright © 2011-2022 走看看