对象内置方法,继承,instanceof添加上(安全使用)
来源:互联网 发布:太师鞭 知乎 编辑:程序博客网 时间:2024/06/10 23:45
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>
0 0
- 对象内置方法,继承,instanceof添加上(安全使用)
- Java--子类与继承,instanceof运算符,成员变量的的隐藏,方法重写,对象的上转型对象
- JavaScript对象与继承教程之内置对象(上)
- Javascript prototype属性使用(给内置对象添加新的方法,方便调用)
- 面向对象--instanceof,equals方法
- JSP 9个内置对象方法使用
- JSP 9个内置对象方法使用
- struts2 使用内置对象的方法
- JSP内置对象上
- javascript基础:对象的继承、添加对象方法
- typeof,instanceof,Object.prototype.toString.call(),js内置对象
- instanceof的使用: 对象与构造函数在原型链上是否有关系
- 内置对象Math方法
- 面向对象内置方法
- 使用apply方法现实对象继承
- 21-JAVA面向对象-继承-final关健字,instanceof关健字
- JSP内置对象(上)
- JSP内置对象(上)
- PHP与数据结构之栈的顺序存储
- JavaSE进阶-Java设计模式
- java.lang.RuntimeException: java.sql.SQLException: Too many parameters: expected 0, was given 1 Quer
- 4.14
- 线性结构——用栈实现波兰表达式求值。
- 对象内置方法,继承,instanceof添加上(安全使用)
- tensorflow的基本用法(六)——神经网络可视化
- 1088. Rational Arithmetic (20)[逻辑题]
- Spring学习之缓存机制EhCache
- react-native新导航组件react-navigation详解
- 无向图的割顶和桥,无向图的双连通分量入门详解及模板
- 碰撞器和触发器
- 创业公司小团队为什么要使用Docker
- hive-1.x在hadoop-2.x上的安装