js面形对象(2)
1.原型与in操作符
console.debug(p1.hasOwnProperty('name'));//false 实例中没有name属性console.debug('name' in p1);//输出true 实例或者原型中有name属性p1.name = 'Lebron';console.debug(p1.hasOwnProperty('name'));//true 实例中有name属性console.debug('name' in p1);//输出true 实例或者原型中有name属性
function hasPrototypeProperty(object,name){return (name in object) && (!object.hasOwnProperty(name));}console.debug( hasPrototypeProperty(p2,'name'));//返回true name属性在原型中p2.name = 'kobe';//实例p2增加name属性console.debug( hasPrototypeProperty(p2,'name'));//返回false name属性不在原型中
function ownPrototypeHasProperty(object,name){while( Object.getPrototypeOf(object).constructor != Object){//object的原型的构造函数不为Objectvar pro = Object.getPrototypeOf(object);//获取这个原型//如果原型中有这个属性if(pro.hasOwnProperty(name)){return true;}//顺着原型向上搜索object = pro;}return false;}var v3 = new Person();console.debug( ownPrototypeHasProperty(v3,'name'));//true v3的原型中有name属性console.debug( ownPrototypeHasProperty(v3,'sex'));//false v3的原型中木有sex属性
console.debug('---------------');function itratePropertyOf(object){for( var per in object){console.debug(per);}}itratePropertyOf(v3);
function Person() {}Person.prototype.name = "Nicholas";Person.prototype.age = 29;Person.prototype.job = "Software Engineer";Person.prototype.sayName = function() {console.debug(this.name);};var keys = Object.keys(Person.prototype);console.debug(keys); //"name,age,job,sayName"
function Person() {}Person.prototype.name = "Nicholas";Person.prototype.age = 29;Person.prototype.job = "Software Engineer";Person.prototype.sayName = function() {console.debug(this.name);};function Per (){console.debug('Per构造函数');}//设置per的原型 使用对象字面量的方式,但是这样会使Per的原型失去construtor(会使用Object() ),所以要制定constructor 为 PerPer.prototype = {name : 'kobe',sayName : function(nameStr){this.name = nameStr;},constructor : Per};var p1 = new Per();var p2 = new Per();console.debug(p1);console.debug(p2);
function Person(){}//Person的原型指向新对象Person.prototype = {constructor: Person,name : "Nicholas",age : 29,job : "Software Engineer",sayName : function () {alert(this.name);}};var friend = new Person();Person.prototype.sayHi = function(){alert("hi");};friend.sayHi(); //"hi" works!
function Person(){}var friend = new Person();//Person的原型对象指向了一个新的对象Person.prototype = {constructor: Person,name : "Nicholas",age : 29,job : "Software Engineer",sayName : function () {alert(this.name);}};//friend.sayName(); //Uncaught TypeError: Object #<Person> has no method 'sayName'//friend对象的原型指针仍然指向的是旧的Person的原型对象导致错误的发生
/**原型模式不仅仅适用于创建自定义类型,原生的引用类型也是采用原型模式创建的**/console.debug(typeof Array.prototype.sort); //"function"console.debug(typeof String.prototype.substring); //"function"//通过原型对象的原型 不仅仅可以取得默认方法的引用,并且可以修改原声对象的引用//下面的代码给基本包装类型String添加一个名为startsWith()的方法String.prototype.startsWith = function(text) {return this.indexOf(text) == 0;};var msg = "Hello world!";console.debug(msg.startsWith("Hello")); //true
//主人 默认名字为kobevar per_1 = new Person();//Dog构造函数function Dog(){}//Dog原型Dog.prototype = {constuctor : Dog,name : 'Hachi',master : per_1,//主人为per_1 per_1的name默认为KobesayName : function(){console.debug(this.name + ' 主人' +this.master.name);}};//实例化两只狗狗var d1 = new Dog();var d2 = new Dog();d1.sayName();//Hachi 主人kobed2.sayName();//Hachi 主人kobed1.master.name = 'Garnett';//改变狗狗1的主人d1.sayName();//Hachi 主人Garnettd2.sayName();//Hachi 主人Garnett 逻辑上不应该更改的吧
function Person(){}Person.prototype = {constructor: Person,name : "Nicholas",age : 29,job : "Software Engineer",friends : ["Shelby", "Court"],sayName : function () {alert(this.name);}};var person1 = new Person();var person2 = new Person();person1.friends.push("Van");alert(person1.friends); //"Shelby,Court,Van"alert(person2.friends); //"Shelby,Court,Van"alert(person1.friends === person2.friends); //true
组合使用构造函数模式和原型模式
function Person(name, age, job){this.name = name;this.age = age;this.job = job;this.friends = ["Shelby", "Court"];}Person.prototype = {constructor: Person,sayName : function () {console(this.name);}};var person1 = new Person("Nicholas", 29, "Software Engineer");var person2 = new Person("Greg", 27, "Doctor");person1.friends.push("Van");console(person1.friends); //"Shelby,Court,Van"console(person2.friends); //"Shelby,Court"console(person1.friends === person2.friends); //falseconsole(person1.sayName === person2.sayName); //true
动态原型模式
js面形对象(2)的更多相关文章
- 程序猿都没对象,JS竟然有对象?
现在做项目基本是套用框架,不论是网上的前端还是后端框架,也会寻找一些封装好的插件拿来即用,但还是希望拿来时最好自己过后再回过头了解里面的原理,学习里面优秀的东西,不论代码封装性,还是小到命名. 好吧, ...
- js中判断对象具体类型
大家可能知道js中判断对象类型可以用typeof来判断.看下面的情况 <script> alert(typeof 1);//number alert(typeof "2" ...
- 浅解析js中的对象
浅解析js中的对象 原文网址:http://www.cnblogs.com/foodoir/p/5971686.html,转载请注明出处. 前面的话: 说到对象,我首先想到的是每到过年过节见长辈的时候 ...
- js中XMLHttpRequest对象实现GET、POST异步传输
js中XMLHttpRequest对象实现GET.POST异步传输 /* * 统一XHR接口 */ function createXHR() { // IE7+,Firefox, Opera, Chr ...
- 170104、js内置对象与原生对象
内置对象与原生对象 内置(Build-in)对象与原生(Naitve)对象的区别在于:前者总是在引擎初始化阶段就被创建好的对象,是后者的一个子集:而后者包括了一些在运行过程中动态创建的对象. 原生对象 ...
- (转)JS获取当前对象大小以及屏幕分辨率等
原文 JS获取当前对象大小以及屏幕分辨率等 <script type="text/javascript">function getInfo(){ var ...
- JavaScript基础17——js的Date对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaScript基础18——js的Array对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaScript学习12 JS中定义对象的几种方式
JavaScript学习12 JS中定义对象的几种方式 JavaScript中没有类的概念,只有对象. 在JavaScript中定义对象可以采用以下几种方式: 1.基于已有对象扩充其属性和方法 2.工 ...
随机推荐
- mysql_create_frm
http://www.cnblogs.com/jiangxu67/p/4755097.html http://www.cnblogs.com/jiangxu67/p/4755097.html http ...
- bzoj2788
明显是一个差分约束系统 对于第一种限制,其实就是x[a]+1<=x[b] x[b]-1<=x[a] 根据三角不等式很容易建图 但这题他比较奇怪,问的是X最多不同取值的个数 根据这张图的特殊 ...
- LA 3516 (计数 DP) Exploring Pyramids
设d(i, j)为连续子序列[i, j]构成数的个数,因为遍历从根节点出发最终要回溯到根节点,所以边界情况是:d(i, i) = 1; 如果s[i] != s[j], d(i, j) = 0 假设第一 ...
- bzoj1832: [AHOI2008]聚会
写过的题... #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- memcache的最佳实践方案。
基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...
- mkswap 把一个分区格式化成为swap交换区
mkswap /dev/sda* //创建此分区为swap 交换分区swapon /dev/sda* //加载交换分区swapoff /dev/sda* //关闭交换分区: 若想要开机自动挂载:vi ...
- 移动设备3G网站制作的detail
说明一下,在此所说的移动设备前端开发是指针对高端智能手机(如Iphone.Android),所以需要对webkit内核的浏览器有一定的了解. 1.webkit内核中的一些私有的meta标签 <m ...
- OpenGL超级宝典第5版&&GLSL法线变换
在GLSL中,有一些情况需要把局部坐标系下的向量或点转换到视点坐标系下,如光照计算时,需要把法向转化到视点坐标系.如果是模型上一点p 转化到视点坐标系下,直接(model-view matrix )* ...
- ASP.NET CS文件中输出JavaScript脚本的3种方法以及区别
Response.Write 与 Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...
- C++实现网格水印之调试笔记(二)
整理了一下要实现的论文Watermarking 3D Polygonal Meshes in the Mesh Spectral Domain,步骤如下: 嵌入水印 à 提取水印 à 优化(网格细分) ...