1.sample o-o class
function obj(){}
obj.prototype.showLog = function(str){
console.log(str);
};
var o = new obj();
o.showLog("something");
2.prototype defination will be override by object instance method or prop
function obj(){
this.m = function(){console.log("obj version");}
}
obj.prototype.m = function(){console.log("prototype version");}
var o = new obj();
o.m();
This reason is because the init sequence :
1 Object members
2 prototpe methods or properties
3 Object methods
3.typeof , instanceof , constructor usage
function obj(){}
var o = new obj();
console.log(typeof o );
console.log(o instanceof obj);
console.log(o.constructor);
4.object clone
function obj(){}
var o = new obj();
var o2 = new o.constructor();//very important when we do not know the type
console.log(o2 instanceof obj);
console.log(o == o2);
5.inheritance
function parent(){}
parent.prototype.show = function(msg){console.log(msg);}
function child(){};
child.prototype.show = parent.prototype.show;
var c = new child();
c.show("hello");
problem :
console.log(c instanceof parent);
result is False!!!
To Fix the Issue , Version 2 :
function parent(){}
parent.prototype.show = function(msg){console.log(msg);}
function child(){};
child.prototype = new parent();
child.prototype.showEx = function(msg){console.log("from showEx " + msg);}
var c = new child();
c.show("hello");
c.showEx("hello");
6.Html Element prototype
prepare HTML :
<div id="parent">
<div id="a">I'm going to be removed.</div>
</div>
Javascript :
HTMLElement.prototype.remove = function (){
if(this.parentNode)
{
this.parentNode.removeChild(this);
}
}
console.log(document.getElementById("a").remove());
7.Extend Array
function list (){}
list.prototype = new Array();
list.prototype.add = function(){for(var i = 0;i < arguments.length;i++)this.push(arguments[i]);}
list.prototype.remove = function(n){for(var i =0 ; i < this.length;i++) if(this[i] == n){this.splice(i,1);}}
var l = new list();
l.add(1,2,3,4);
l.remove(3);
console.log(l);
8.Extend Number Class
Number.prototype.add = function(){var result = 0; for(var i = 0 ;i < arguments.length;i++) result += arguments[i]; return result;};
var n = 5;
console.log(n.add(5,2,3,4,9));
分享到:
相关推荐
Javascript Object Oriented Programming 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请...
javascript object oriented 面向对象编程初步.docx
Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust ...
The Principles of Object Oriented.JavaScript
If you've used a more traditional object-oriented language, such as C++ or Java, JavaScript probably doesn't seem object-oriented at all. It has no concept of classes, and you don't even need to ...
Object-Oriented JavaScript
prototype-based object-oriented programming language. Once dismissed as a toy for designers to make things such as rollover buttons, today this interesting and unique language is back, stronger than...
用 new Object() 来创建对象在javascript里有几种创建对象的方法,在不同的场合可用不同的方法.最简单的就是用 new 操作符,例如: 代码如下:[removed] <!– person = new Object() person.name = ...
Object Oriented JavaScript, 3rd Edition Learn everything you need to know about object-oriented JavaScript with this comprehensive guide. Enter the world of cutting-edge development! About This Book ...
JavaScript Mancy Object Oriented Programming 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有...
JavaScript Mancy Object Oriented Programming 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
JavaScript Mancy Object Oriented Programming 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Learn everything you need to know about object-oriented JavaScript with this comprehensive guide. Enter the world of cutting-edge development! About This Book This book has been updated to cover all ...
JavaScript Mancy Object Oriented Programming 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
JavaScript经典书籍, JS的童鞋值得收藏
Mastering JavaScript Object-Oriented Programming 2016 | ISBN: 1785889109 | English | 287 pages | PDF | 1.9 MB With this book, we'll provide you with a comprehensive overview of OOP principles in ...
Object Oriented JavaScript(3rd) 英文无水印pdf 第3版 pdf使用FoxitReader和PDF-XChangeViewer测试可以打开
Mastering JavaScript Object-Oriented Programming 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如...
The Principles of Object-Oriented JavaScript 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
This book shows you everything you need to learn object oriented JavaScript, demonstrating valuable techniques and strategies that you can use to fully experience the impressive capabilities of the ...