对象内置方法,继承,instanceof添加上(安全使用)
2017-04-14 22:55
513 查看
instanceof添加上(安全使用)
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style type="text/css"> * {margin: 0; padding: 0;} a {text-decoration: none;} ul,li {list-style: none;} body {font-family: "Microsoft yahei";} </style> </head> <body> <script type="text/javascript"> /* instanceof 检测创建的对象是由谁实例得到的 new 出来的 ####构造函数单独执行 是没有任何意义的 只有在new的时候才会有意义 */ // function Person(names,age){ // this.names = names; // this.age = age; // }; // Person.prototype.eat = function(){ // console.log(this.names); // }; // Person.prototype.type = "bitch"; // var p1 = new Person("pr",1213); // console.log(p1.names+"====="+p1.age); // 安全使用添加instanceof function Person(name,age){ if(this instanceof Person){ //作防止措施.==>防止this指向window this.names = name; this.age = age; }else{ return new Person(name,age); } }; // var p1 = Person("pr",123); //可以不用new var p1 = new Person("pr",123); alert(p1.names) </script> </body> </html>
对象内置方法
数据类型 object -- json object constructor 判断来自哪一个构造函数 in 判断一个属性是否存在于对象中(不分是不是继承的属性或者方法) hasOwnProperty 判断自有属性 isPrototypeOf 判断来自哪一个原型链 propertyIsEnumerable 属性是否可以被for in toString 转成一个字符串 valueOf 生成一个副本 引用地址的一样的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Author" content=" "> <title>Document</title> <style type="text/css"> *{margin: 0;padding: 0;} a{text-decoration: none;} ul,li{list-style: none;} body{font-size: 14px;font-family: "微软雅黑";} </style> </head> <body> <script type="text/javascript"> /* 数据类型 object -- json object constructor 判断来自哪一个构造函数 in 判断一个属性是否存在于对象中(不分是不是继承的属性或者方法) hasOwnProperty 判断自有属性 isPrototypeOf 判断来自哪一个原型链 propertyIsEnumerable 属性是否可以被for in toString 转成一个字符串 valueOf 生成一个副本 引用地址的一样的 */ function Person(name,age){ //自有属性 this.name = name; this.age = age; this.say = function(){ alert(1); }; }; //扩展属性 Person.prototype.eat = function(){ alert("eat"); }; Person.prototype.type = "人类"; var p1 = new Person("xq",18); var p2 = new Person("cc",20); for(var k in Person.prototype){ console.log(Person.prototype[k]); }; // alert(Person.prototype.constructor); /* in hasOwnProperty isPrototypeOf */ // alert(p1.eat === p2.eat);//true // alert(p1.say === p2.say);//false // alert("eat" in p1);//true // alert("say" in p1);//true // alert(p1.hasOwnProperty("name"));//true 自己的 // alert(p1.hasOwnProperty("type"));//false 原型链 // alert(p1.hasOwnProperty("valueOf"));//false object // alert(Person.prototype.isPrototypeOf(p1));//true // var json1 = { // name:'xq' // }; // var json2 = json1.valueOf(); // alert(json1 === json2); // json2.name = "茅草屋"; // alert(json1.name); // var date = new Date(); // var d = date.toLocaleString(); // document.write(d); </script> </body> </html>
extend继承拷贝
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style type="text/css"> * {margin: 0; padding: 0;} a {text-decoration: none;} ul,li {list-style: none;} body {font-family: "Microsoft yahei";} </style> </head> <body> <script type="text/javascript"> /* 继承extends 子类可以继承父类 子类可以使用父类的方法或者属性 子类不可以影响父类(重写) */ function Person(name){ if(this instanceof Person){ this.name = name; }else{ return new Person(name); } }; Person.prototype.showName = function(){ alert(this.name); }; var p = Person("goudan"); // p.showName(); function Child(name,age){ if(this instanceof Child){ this.age = age; this.name = name; }else{ return new Child(name,age); } }; // Child.prototype = Person.prototype; //子类不可以影响父类 // Child.prototype = extend(Person.prototype); //相当于字面量扩展 ===>会把constructor改变成Object extend(Person.prototype,Child.prototype); Child.prototype.showAge = function(){ alert(this.age); }; var c = Child("ergou",18); // c.showAge(); c.showName(); // alert(c.constructor);//Object // p.showAge();//子类不可以影响父类 function extend(obj,childObj){ //正确的做法. // var childObj = {}; for(var k in obj){ // 继承父类的属性,方法. 原型链的 childObj[k] = obj[k]; }; // return childObj; }; </script> </body> </html>
类继承
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style type="text/css"> * {margin: 0; padding: 0;} a {text-decoration: none;} ul,li {list-style: none;} body {font-family: "Microsoft yahei";} </style> </head> <body> <script type="text/javascript"> /* 继承extends 子类可以继承父类 子类可以使用父类的方法或者属性 !!做到子类不可以影响父类(重写) */ function Person(name){ // if(this instanceof Person){ this.name = name; // }else{ // return new Person(name); // } }; Person.prototype.showName = function(){ alert(this.name); }; var p = new Person("goudan"); function Fn(){ }; Fn.prototype = Person.prototype;//==> Fn的constructor=>Person Child.prototype = new Fn(); //onstructor=>Person Child.prototype.constructor = Child;//手动改回来. function Child(name,age){ if(this instanceof Child){ this.age = age; Person.call(this,name); }else{ return new Child(name,age); } }; Child.prototype.showAge = function(){ alert(this.age); }; var c = Child("ergou",18); console.log(c.name); // c.showAge(); c.showName(); alert(c.constructor)//构造器一层一层找 // p.showAge(); </script> </body> </html>
相关文章推荐
- Javascript prototype属性使用(给内置对象添加新的方法,方便调用)
- JSP内置对象request的方法的使用
- javascript对象之内置对象Math使用方法
- 使用apply方法实现javascript中的对象继承
- Python类的定义、继承及类对象使用方法简明教程
- java web 程序---内置对象application的log方法的使用
- 使用Java继承与泛型特性创建高效与安全的DAO对象
- 给内置对象或自定义对象添加存取器属性(getter setter)的方法总结
- 使用apply方法实现javascript中的对象继承
- 在创建Vector对象的时候,添加了几个元素,然后使用get(int index)方法,结果显示出错了;
- Python类的定义、继承及类对象使用方法简明教程
- JSP内置对象request的方法的使用
- 基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
- JS 实现 创建类、继承、方法添加、对象克隆、数组封装操作
- 使用apply方法现实对象继承
- 使用 AddRange 方法将多个 ListItem 对象添加到集合
- JSP内置对象request的方法的使用
- JSP 9个内置对象方法使用
- 使用DateAdd方法向指定日期添加一段时间间隔,使用TimeSpan对象获取时间间隔
- js对象中的方法能不能使用原型继承