SlideShare a Scribd company logo
波公司驰 2015 年度技 交流沙 ——术 龙
JavaScript 大 之路师
JavaScript 原生 的研究与 承模式 践链 继 实
• CJDP3 框架解析及改 思路进
• 一的统 实时 / 批量数据采集平台研究
• 基于用 活定制的 表技 分析及 用研究户灵 报 术 应
• 数据可 化技 在支付系 中的 用视 术 统 应
• 互 网行 的技 点联 业 术热 纵览
• JavaScript 程深入编
• web 新 :基于开发 趋势 JavaScript 的 web 服
务
Original Regression
• 于后端工程 。。。对 师
• 于前端工程 。。。对 师
• 于人力部和 合部的同事???对 综
Talk is less; show me the code.
document.body.innerHTML = '<div id="div1">CMB</div><div
id="div2">ICBC</div><div id="div3">BBC</div><div
id="div4">ABC</div>';
for (var i = 4; i >= 1; i--) {
document.getElementById('div' + i).
addEventListener('click', function(){
alert(i);
});
}
for (var i = 4; i >= 1; i--) {
!function(i){
document.getElementById('div' + i).
addEventListener('click', function(){
alert(i);
});
}(i);
}
Why JavaScript ?
JavaScript is ......
<div id="div1">
<input type="button" onclick="append_p()">
<p id="p2"> 是一个段落这 </p>
</div>
<script>
function append_p(){
var para=document.createElement("p");
var node=document.createTextNode(" 是新段落。这 ");
para.appendChild(node);
var element=document.getElementById("div1");
element.appendChild(para);
}
</script>
JavaScript is ......
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Why JavaScript ?
• HTML5 和 JavaScript 在 器端首屈一指浏览
。
– Where is Flash , Sliverlight and Java FX ?
• JavaScript 在服 器端也有一席之地。务
– NodeJS ,事件 、 非阻塞驱动 I/O 模型
JavaScript OOP
封装 承继 多态
Java Code
public class Person{
private String firstname, lastname;
Person(String firstname, String lastname){
this.firstname = firstname;
this.lastname = lastname;
}
public String getName(){
return this.firstname + this.lastname;
}
static void walking(){
Systems.out.print("I am working");
}
}
......
Person p = new Person("Rex","Gao");
String fullname = p.getName();
Person.walking();
JavaScript 原始类型
• number
• string
• boolean
• null
• undefined
• Object
– function
– array
– date
JavaScript Solution
var Person = {
firstname : "first",
lastname : "last",
getname : function(){
return this.firstname + this.lastname;
},
walking : function(){
console.log("I am walking");
}
};
......
Person.firstname = 'Rex';
Person.lastname = 'Gao';
var fullname = Person.getname();
console.log(fullname);
Person.walking();
Has it been done?
• 可是 ......
– 象的事例 ?对 呢 new Person1 , Person2
– 承 ?继 呢 Student 类, Teacher 类
– 多 ? 重态呢 载 getName 函数
– 造函数 ?构 呢
– 私有和公有 量 ?变 呢
– 静 量和函数 ?态变 呢
• 怎么破?
承继
var Student = Object.create(Person);
Student.getName = function(){
return "Stu Name: " + this.firstname + this.lastname;
}
Student.doHomework = function(){
console.log("I am doing my homework");
}
var fullname = Student.getName();
console.log(fullname);
Student.walking();
Student.doHomework();
造函数法构
var Person = function(fname,lname,sex){
var firstname = fname;
var lastname = lname;
this.getName = function(){
return firstname + lastname;
}
this.sexual = sex;
Person.prototype.sayHello = function(){
if(this.sexual == "male"){
console.log("Hey, I am a man!")
}else{
console.log("Hi, I am a woman~")
}
}
}
var p = new Person('Rex','Gao','male');
console.log(p.getName());
p.sayHello();
P
[Obj]
getName()
sexual
[Person.prototype]
sayHello()
__proto__ __proto__
Object
通过 new 操作符 建一个 象:构 对
A. 建一个新 象创 对
B. 将 造函数的作用域 新 象构 赋给 对
C. 行 造函数中的代执 构 码
D. 返回 个新 象这 对
再 承谈继
var Student= function(fname,lname,sex){
Person.apply(this, arguments);
};
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
Student.prototype.sayHello = function(){
if(this.sexual == "male"){
console.log("Momoda, I am a boy!")
}else{
console.log("Oba, I am a girl~")
}
}
Student.laugh = function(){
console.log("Hahaha~~");
}
var s = new Student('Rex','Gao','male');
console.log(s.getName());
s.sayHello();
Student.laugh();
S
[Obj]
getName()
sexual
[Student.prototype]
sayHello()
__proto__
__proto__
Object
[Person.prototype]
sayHello()
__proto__
就是这 JavaScript 原生链
有一些不完美还
var Teacher= function(fname,lname,sex){
if(!(this instanceof Teacher)){
throw new Error('Do not invoke without new');
}
Person.apply(this, arguments);
};
inherit(Teacher,Person);
function inherit(subClass, superClass){
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
}
Object.freeze(Teacher);
Object.freeze(Teacher.prototype);
然而天地本就不全 ......
个引深(两 jQuery )
$('#selector').get(0);
var $ = aQuery = function(selector) {
// 强制 象为对
if (!(this instanceof aQuery)) {
return new aQuery(selector);
}
var elem = document.getElementById(/[^#].*/.exec(selector)[0]);
this.length = 1;
this[0] = elem;
this.context = document;
this.selector = selector;
this.get = function(num) {
return this[num];
}
return this;
}
$('selector').each(); $.each();
aQuery.each = aQuery.prototype.each = function()......
Javascript OOP

More Related Content

Similar to Javascript OOP (20)

PDF
潜力无限的编程语言Javascript
jay li
 
PPT
Node.js在淘宝的应用实践
taobao.com
 
PPTX
前端测试
Zheng Biao
 
PPTX
前端测试
frontwindysky
 
PPTX
Script with engine
Webrebuild
 
PDF
Node way
Ethan Zhang
 
PPT
Javascript之昨是今非
Tony Deng
 
PDF
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
Johnny Sung
 
PPT
Js培训
yiditushe
 
KEY
D2_Node在淘宝的应用实践
Jackson Tian
 
PPTX
异步编程与浏览器执行模型
keelii
 
PDF
線上埋碼資料收集實作
FEG
 
PDF
D2_node在淘宝的应用实践_pdf版
Jackson Tian
 
PDF
in in der 響應式編程
景隆 張
 
PDF
基于Spring batch的大数据量并行处理
Jacky Chi
 
PDF
Javascript autoload
jay li
 
PPT
Html5和css3入门
Xiujun Ma
 
PDF
Gulp.js 自動化前端任務流程
洧杰 廖
 
PPTX
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
Duran Hsieh
 
PDF
Javascript
Ryan Chung
 
潜力无限的编程语言Javascript
jay li
 
Node.js在淘宝的应用实践
taobao.com
 
前端测试
Zheng Biao
 
前端测试
frontwindysky
 
Script with engine
Webrebuild
 
Node way
Ethan Zhang
 
Javascript之昨是今非
Tony Deng
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
Johnny Sung
 
Js培训
yiditushe
 
D2_Node在淘宝的应用实践
Jackson Tian
 
异步编程与浏览器执行模型
keelii
 
線上埋碼資料收集實作
FEG
 
D2_node在淘宝的应用实践_pdf版
Jackson Tian
 
in in der 響應式編程
景隆 張
 
基于Spring batch的大数据量并行处理
Jacky Chi
 
Javascript autoload
jay li
 
Html5和css3入门
Xiujun Ma
 
Gulp.js 自動化前端任務流程
洧杰 廖
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
Duran Hsieh
 
Javascript
Ryan Chung
 

Javascript OOP

  • 1. 波公司驰 2015 年度技 交流沙 ——术 龙 JavaScript 大 之路师 JavaScript 原生 的研究与 承模式 践链 继 实
  • 2. • CJDP3 框架解析及改 思路进 • 一的统 实时 / 批量数据采集平台研究 • 基于用 活定制的 表技 分析及 用研究户灵 报 术 应 • 数据可 化技 在支付系 中的 用视 术 统 应 • 互 网行 的技 点联 业 术热 纵览 • JavaScript 程深入编 • web 新 :基于开发 趋势 JavaScript 的 web 服 务
  • 3. Original Regression • 于后端工程 。。。对 师 • 于前端工程 。。。对 师 • 于人力部和 合部的同事???对 综
  • 4. Talk is less; show me the code.
  • 5. document.body.innerHTML = '<div id="div1">CMB</div><div id="div2">ICBC</div><div id="div3">BBC</div><div id="div4">ABC</div>'; for (var i = 4; i >= 1; i--) { document.getElementById('div' + i). addEventListener('click', function(){ alert(i); }); } for (var i = 4; i >= 1; i--) { !function(i){ document.getElementById('div' + i). addEventListener('click', function(){ alert(i); }); }(i); }
  • 7. JavaScript is ...... <div id="div1"> <input type="button" onclick="append_p()"> <p id="p2"> 是一个段落这 </p> </div> <script> function append_p(){ var para=document.createElement("p"); var node=document.createTextNode(" 是新段落。这 "); para.appendChild(node); var element=document.getElementById("div1"); element.appendChild(para); } </script>
  • 8. JavaScript is ...... var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');
  • 9. Why JavaScript ? • HTML5 和 JavaScript 在 器端首屈一指浏览 。 – Where is Flash , Sliverlight and Java FX ? • JavaScript 在服 器端也有一席之地。务 – NodeJS ,事件 、 非阻塞驱动 I/O 模型
  • 12. Java Code public class Person{ private String firstname, lastname; Person(String firstname, String lastname){ this.firstname = firstname; this.lastname = lastname; } public String getName(){ return this.firstname + this.lastname; } static void walking(){ Systems.out.print("I am working"); } } ...... Person p = new Person("Rex","Gao"); String fullname = p.getName(); Person.walking();
  • 13. JavaScript 原始类型 • number • string • boolean • null • undefined • Object – function – array – date
  • 14. JavaScript Solution var Person = { firstname : "first", lastname : "last", getname : function(){ return this.firstname + this.lastname; }, walking : function(){ console.log("I am walking"); } }; ...... Person.firstname = 'Rex'; Person.lastname = 'Gao'; var fullname = Person.getname(); console.log(fullname); Person.walking();
  • 15. Has it been done? • 可是 ...... – 象的事例 ?对 呢 new Person1 , Person2 – 承 ?继 呢 Student 类, Teacher 类 – 多 ? 重态呢 载 getName 函数 – 造函数 ?构 呢 – 私有和公有 量 ?变 呢 – 静 量和函数 ?态变 呢 • 怎么破?
  • 16. 承继 var Student = Object.create(Person); Student.getName = function(){ return "Stu Name: " + this.firstname + this.lastname; } Student.doHomework = function(){ console.log("I am doing my homework"); } var fullname = Student.getName(); console.log(fullname); Student.walking(); Student.doHomework();
  • 17. 造函数法构 var Person = function(fname,lname,sex){ var firstname = fname; var lastname = lname; this.getName = function(){ return firstname + lastname; } this.sexual = sex; Person.prototype.sayHello = function(){ if(this.sexual == "male"){ console.log("Hey, I am a man!") }else{ console.log("Hi, I am a woman~") } } } var p = new Person('Rex','Gao','male'); console.log(p.getName()); p.sayHello();
  • 18. P [Obj] getName() sexual [Person.prototype] sayHello() __proto__ __proto__ Object 通过 new 操作符 建一个 象:构 对 A. 建一个新 象创 对 B. 将 造函数的作用域 新 象构 赋给 对 C. 行 造函数中的代执 构 码 D. 返回 个新 象这 对
  • 19. 再 承谈继 var Student= function(fname,lname,sex){ Person.apply(this, arguments); }; Student.prototype = Object.create(Person.prototype); Student.prototype.constructor = Student; Student.prototype.sayHello = function(){ if(this.sexual == "male"){ console.log("Momoda, I am a boy!") }else{ console.log("Oba, I am a girl~") } } Student.laugh = function(){ console.log("Hahaha~~"); } var s = new Student('Rex','Gao','male'); console.log(s.getName()); s.sayHello(); Student.laugh();
  • 22. 有一些不完美还 var Teacher= function(fname,lname,sex){ if(!(this instanceof Teacher)){ throw new Error('Do not invoke without new'); } Person.apply(this, arguments); }; inherit(Teacher,Person); function inherit(subClass, superClass){ subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; } Object.freeze(Teacher); Object.freeze(Teacher.prototype);
  • 24. 个引深(两 jQuery ) $('#selector').get(0); var $ = aQuery = function(selector) { // 强制 象为对 if (!(this instanceof aQuery)) { return new aQuery(selector); } var elem = document.getElementById(/[^#].*/.exec(selector)[0]); this.length = 1; this[0] = elem; this.context = document; this.selector = selector; this.get = function(num) { return this[num]; } return this; } $('selector').each(); $.each(); aQuery.each = aQuery.prototype.each = function()......