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.工 ...
随机推荐
- BNU 4188 Superprime Rib【BFS】
题意:给出n,输出n位超级质数,超级质数的定义为“依次去掉右边一位后仍然为质数的数” 因为一个n位质数去掉右边一位数之后仍然为质数,说明它是由n-1位超级质数演变而来的, 同理,n-1位超级质数也由n ...
- Oracle中使用escape关键字实现like匹配特殊字符,以及&字符的转义
转:http://blog.chinaunix.net/uid-26896647-id-3433968.html 问题描述:如果在一个表中的一个字段上存在'&', '_', '%'这样的特 ...
- qq互联(connect.qq.com)取用户信息的方法
<?php //应用的APPID$app_id = "YOUR_APP_ID";//应用的APPKEY$app_secret = "YOUR_APP_KEY&quo ...
- Java [Leetcode 136]Single Number
题目描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Java [Leetcode 102]Binary Tree Level Order Traversal
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- CoreData 基本操作方法封装
转:http://blog.csdn.net/marujunyy/article/details/18500523 为了方便使用CoreData 封装了几个扩展类,使用方法和类文件如下: //首先需要 ...
- 顶 企业站常用css横向导航菜单
<!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/T ...
- SQL之50个常用的SQL语句
50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...
- [Everyday Mathematics]20150110
试证: $$\bex \vlm{n}\frac{\ln^2n}{n}\sum_{k=2}^{n-2}\frac{1}{\ln k\cdot \ln(n-k)}=1. \eex$$