SlideShare a Scribd company logo
www.ktrick.com
A-3
Javascript OOP for XPages Developers
Kazunori Tatsuki @ Ktrick Co,. Ltd.
• Name: Kazunori Tatsuki
• Company: KTrick Co,. Ltd.
• Title: Owner of KTrick Co,. Ltd.
• My Experience:
- 7 years development experience as IBM Vender in US.
- XPages and all other domino related languages, C, C++,
Objective-C, Java, PHP etc.
Introduction
Are you struggling with developing
XPages application?
Why?
Javascript?
Client side Javascript…
Sever side Javascript…
What’s different?
Nowadays Javascript sample code is like this …
var func1 = function(){
alert("hello!");
}
var myButton = new Button({
label: "Show me!",
onClick: function(){
myDialog.set("content", “Hello!");
myDialog.show();
}
}, "progbutton").startup();
Javascript used to be like this…
function func1(param){
alert( param);
}
It is easier to understand, isn’t it?
Why has it changed?
Have you ever seen this?
( function() {
//contents here…
}() );
Hmmm, Too much parenthesis…
Actually these are all different meaning.
Javascript looks complicated.
but
Don’t give up!
In this session,
I would like you to understand the structure of
Javascript by breaking down the source code one by one.
Object Oriented Programming
What’s the benefits of using OOP?
Benefits of OOP
・Reusability
・ Refactoring
・ Extensibility
・ Maintenance
・ Efficiency
As a result,
you can reduce the time and cost for development and testing.
OOP
What do you imagine from this word?
OOP
Class
Delegate
OverloadPolymorphism
Java
C++
Abstract
class
C#
Override
Inheritance
Derived class
Instance
UML
If you already have OOP experience, you may think of
「 Class 」
Is there class in Javascript?
Nope
How can we develop OOP without class?
The answer is…
Prototype Chain !!
To begin with, Javascript only has
Object.
sub Class (type)
By the way, Class is like this…
Super Class (type)
Object (Instance)
Object (Instance)
Object (Instance)
NEW
NEW
NEW
Class is like ‘type’.
You define the class as the
abstract type for instance. Then
you can generate the instances
from the classes.
By doing this, you can generate
same type objects (instances)
easily ,which have the same
functions and attributes.
Again, Javascript doesn’t have Class structure…
then…
Javascript Prototype chain is like this…
Object (Instance)
Object (Instance)
Object (Instance)
Object (Instance)
Dele
gate
Dele
gate
Dele
gate
You can connect the objects
and objects like chain.
Then if the original object
doesn’t have the specific
methods or variables, then
Javascript delegates to search
in the other object until they
are found.
Object (Instance)
Dele
gate
Let’s take a look at the actual source code.
※Basically please consider that the sample codes are written in client side Javascript
because server side Javascript cannot use some methods like alert().
However the theories and structures are the same!
Basic lesson.
How to generate Object?
var obj = { };
That’s it. An empty object was generated.
Object structure
var obj = {
key: value
};
Object can have Properties
(the set of Key and value)
That’s it!
var obj = {
name : “Apple”,
size : 500
};
alert(obj.name);
(i.e.)
var obj = {
func1 : function( ) {alert(“hoge”); }
};
obj.func1(); // Output is “hoge”
In Javascript, even the method is a “value”
var func = function( ){};
A method is a value,
therefore you can set it to
a variable.
Of course, you can
operate variables as usual.
(i.e) You can set the method to object
property.
Finally, explanation of Prototype Chain !!
It’s THE Javascript OOP
There are two important keywords below when learning
Javascript Prototype Chain.
➲ __proto__ ➲prototype
It’s very important to understand what is different from each other
to understand Javascript OOP.
➲ __proto__
__proto__
・[Rule] All objects in Javascript have [[Prototype]] internally.
However some browser application like Chrome, Firefox etc allow
you to access the [[Prototype]] by using __proto__ property
instead.
Internal means that you basically
cannot access to the [[Prototype]]
Like this…
Object (Instance)
Object (Instance)
Object (Instance)
Object (Instance)
Dele
gate
Dele
gate
Dele
gate
__proto__
__proto__
__proto__
__proto__
Sample code (This sample code works in Chrome & Firefox since allowing you to access by
using “__proto__” )
However this code
alert “It is Apple”
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Object is empty…
Confirm the order of source code(1)
It alerts “It is Apple”
Generates object named obj.
It has name and callMe
property.
Generates object named objApple.
name property is ”Apple”.
Generates object named
objBlackbox.
There is no property.
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Confirm the order of source code (2)
Set “obj” into
objApple.__proto_
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Generate object named obj.
It has name and callMe
property.
Generate object named objApple.
name property is ”Apple”.
Generate object named
objBlackbox.
There is no property.
Set “objApple” int
objBlackbox.__proto__
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Set “obj” into
objApple.__proto_
Generate object named obj.
It has name and callMe
property.
Generate object named objApple.
name property is ”Apple”.
Generate object named
objBlackbox.
There is no property.
Confirm the order of source code (3)
Confirm the method call (1)
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(1) There is no callMe()
method in objBlackbox
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Confirm the method call (2)
(2)Search object stored in __proto__ (objApple)
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Confirm the method call (3)
(1) There is no callMe()
method in objBlackbox
(3) There is no callMe()
method in objApple neither.
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(2)Search object stored in __proto__ (objApple)
(1) There is no callMe()
method in objBlackbox
Confirm the method call (4)
(4) Serach object stored in __proto__ (obj)
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Confirm the method call (5)
(3) There is no callMe()
method in objApple neither.
(2)Search object stored in __proto__ (objApple)
(1) There is no callMe()
method in objBlackbox
(5) Found callMe() method in obj!var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(4) Serach object stored in __proto__ (obj)
Confirm the method call (6)
(3) There is no callMe()
method in objApple neither.
(2)Search object stored in __proto__ (objApple)
(1) There is no callMe()
method in objBlackbox
(1) callMe() method is called.
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Confirm the attribute call (1)
(2) Refer “name” property
※”this” means “object itself.”
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
Confirm the attribute call (2)
(1) callMe() method is called.
(3) There is no “name”
property in objBlackbox
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(2) Refer “name” property
※”this” means “object itself.”
Confirm the attribute call (3)
(1) callMe() method is called.
(4) Search the object stored in __proto__ (objApple)
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(3) There is no “name”
property in objBlackbox
(2) Refer “name” property
※”this” means “object itself.”
Confirm the attribute call (4)
(1) callMe() method is called.
(5) Found ”name“ in objApple !
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(4) Search the object stored in __proto__ (objApple)
(3) There is no “name”
property in objBlackbox
(2) Refer “name” property
※”this” means “object itself.”
Confirm the attribute call (5)
(1) callMe() method is called.
(6) There is “name” property in obj as
well. However not referred
var obj = {
name : “none”,
callMe : function() { alert(“It is ” + this.name); }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
var objBlackbox = { };
objBlackbox.__proto__ = objApple;
objBlackbox.callMe();
(5) Found ”name“ in objApple !
(4) Search the object stored in __proto__ (objApple)
(3) There is no “name”
property in objBlackbox
(2) Refer “name” property
※”this” means “object itself.”
Confirm the attribute call (6)
(1) callMe() method is called.
Like in the sample code, if the
original object does not have the
methods or attributes, then
Javascript delegates to the chained
object to search them via __proto__
until found
This is
Prototype Chain !
Object (Instance)
Object (Instance)
Object (Instance)
Object (Instance)
Dele
gate
Dele
gate
Dele
gate
Not
Found
Found
Not
Found
Not
Found
Now is it ok to understand __proto__ ?
Wait a minutes…
__proto__ (aka [[Prototype]]) is just internal property.
Then we should not use the code like below...
objApple.__proto__ = obj;
Is it true?
Yes, Do not use it!
In fact I used the code like below as an example so that I can explain
the internal mechanism more clearly.
However, there is an alternative way to achieve the same logic.
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
The answer is using “New” operation
(i.e.)This is the sample code equivalent to the previous sample.
var obj = new Frout(“Apple”);
Breakdown
of this code
later on.
var Frout = function( pname ) {
this.name = pname;
}
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
➲ prototype
Before moving forward, there is an another keyword
called ”prototype”
It’s very important to understand what is different from __proto__!
prototype
・[Rule] All function objects have prototype property.
“function object”
Hmm, New keyword again...
But don’t worry. You already know this.
Function Object is like this…
It was already explained in previous slide.
Function(method) can be value.
var func = function( ){};
All Function Objects have prototype property.
Again,
For example,
var func = function( ){};
alert( func.prototype );
The second line above does not output “undefined”. This means prototype exists as the default.
However after defining the function object, the prototype refer just empty object.
Then, How to use prototype?
The answer is…
Prototype is used with new operation when the object is generated!
Confirm by source code
(This is the sample code equivalent to previous sample for __proto__)
Define Constructor
Generates Function Object.
name is undefined at this
moment.
Set the method to the
prototype property of Frout
object.
Generate objApple object by
using new operation.
By the prototype chain,
objApple can call the callMe()
method of Frout prototype
var Frout = function( pname ) {
this.name = pname;
}
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
objApple.callMe(); Alert 「It is Apple」
※ it is not
“objApple.prototype.callMe() “
Explain in the
next slide
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj;
Try to breakdown the process of new operation artificially.
var objApple = new Frout(“Apple”);
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj;
(1) Generates object.
var objApple = new Frout(“Apple”);
Try to breakdown the process of new operation artificially.
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj;
(2) Set prototype property into __proto__.
Frout.prototype
Has callMe()
method.
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
Try to breakdown the process of new operation artificially.
(1) Generate object.
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj;
(3) Execute constructor function.
Constructor
function is
executed only
when function
object is
generated.
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
Try to breakdown the process of new operation artificially.
(2) Set prototype property into __proto__.
Frout.prototype
Has callMe()
method.
(1) Generate object.
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj; (4) Returns the object.
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
Try to breakdown the process of new operation artificially.
(3) Execute constructor function.
Constructor
function is
executed only
when function
object is
generated.
(2) Set prototype property into __proto__.
Frout.prototype
Has callMe()
method.
(1) Generate object.
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj;
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
var objApple = new Frout(“Apple”);
Try to breakdown the process of new operation artificially.
(4) Return the object.
(3) Execute constructor function.
Constructor
function is
executed only
when function
object is
generated.
(2) Set prototype property into __proto__.
Frout.prototype
Has callMe()
method.
(1) Generate object.
The sample code using for
explanation of __proto__
var obj = {};
obj.__proto__ = Frout.prototype;
Frout.apply(obj, arguments );
return obj;
The breakdown process of new
operation artificially.
If you replace obj to Frout.prototype,
then both objects have callMe()
Frout.prototype.callMe = function() {…};
var obj = {
name : “none”,
callMe : function() { … }
};
var objApple = { name : “Apple”};
objApple.__proto__ = obj;
Difference between __proto__ and prototype
Prototype is used when
object is generated by
new operation to be
stored in __proto__.
Used by prototype
search mechanism
internally.
By the new operation,
object prototype is
automatically stored.
__proto__ prototype
To make a
prototype chain,
use this prototype.
__proto__ is just needed
to understand the internal
mechanism.
Use new and prototype for
OOP.
So, a little Brain Teaser
Frout object preparation
and if we describe
We don’t want to write this, correct? It is not useful
var Frout = function( name ) {
this.name = name;
}
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var Frout = function( name ) {
this.name = name;
this.callMe = function() { alert(“It is ” + this.name); };
}
From an OOP perspective this is bad
But let’s continue…
The reason:
var Frout = function( name ) {
this.name = name;
this.callMe = function() { alert(“It is ” + this.name); };
}
In this code, without a prototype, it will be defined inside the constructor
In other words:
Everytime new is called to create this object, callMe is also instantiated.
In Contrast, methods defined in the prototype, falls within the prototype chain inconsistently
Memory efficiency is better because the mechanism allows reference to the same memory when new objects are
created.
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
Practical Use
From the sample code earlier:
This function of brackets is integral to what kind of work?
( function() {
//code
}() );
Arriving at the conclusion
Calling an anonymous function immediately
We are going to do this
Immediately after the function
object is returned, we're calling
the function.
Create function
Object。
( function() {
//code・・・
}() );
Anonymous funtion
with no variable
This was done through a very complicated process。
This is used as such that when the page is loaded, it processes only once。
There are 2 parts?
//Immediate function1
(function () {
//code・・・
}());
//Immediate function2
(function () {
//code・・・
})();
Both are immediate function. In
JSLint(http://www.jslint.com/) the
former code seems to be preferred
Practical Use 2…
the result…
var Frout = function( name ) {
this.name = name;
}
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
var objApple = new Frout(“Apple”);
alert( objApple.name );
You should not be
able see if it was
private variable
Is this a
private variable?
protected variable?
public variable?
To be considered OOP, don’t we want to hide the private variable?
(Naturally)we are able to see orz
but wait a minute…
I modified something like this
var FroutModel = (function() {
// private variable
var name = “none";
// Constructor
var cls = function(pname){name = pname;};
// public method
cls.prototype.callMe = function() {alert("This is "+name);};
return cls;
} () );
var Frout = function( name ) {
this.name = name;
}
Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
Before re edit
var FroutModel = (function() {
// private variable
var name = “none";
// Constructor
var cls = function(pname){name = pname;};
// public method
cls.prototype.callMe = function() {alert("This is "+name);};
return cls;
} () );
var objApple = new FroutModel(“Apple”);
alert( objApple.name );
objApple.callMe();
To make sure that
undefined is hidden
Can be
successfully called
Somehow it very much looks like a class
In addition to class inheritance, you can implement like this:
var FroutModel = (function() {
// private variable
var name = “none";
// Constructor
var cls = function(pname){name = pname;};
// Parent class inheritance
cls.prototype = new BaseModel();
// public method
cls.prototype.callMe = function() {alert("This is "+name);};
return cls;
} () );
Like this:
BaseModel object and
FroutModel object are
doing the same
implementation
Below are some descriptions about the code up to now. Please challenge yourself by solving the
puzzle
var FroutModel = (function() {
// private variable
var name = “none";
// constructor
var cls = function(pname){name = pname;};
// parent class inheritance
cls.prototype = new BaseModel();
// public method
cls.prototype.callMe = function() {alert("This is "+name);};
return cls;
} () );
Hint: Scope chain cannot be seen
from the outside
Hint:Generate another
object inside the FroutModel
function
Hint: Returns the object that
was generated in the
FroutModel
Hint: Immediate
function
This time、
XPages discussion has not even come out yet…
Now, Javascript has two types:
Client-side Javascript
Server-side Javascript
Both have the concept of class
Lets try the last class’s implementation of SSJS. It’s also useful to copy to script lib
var AppModel = (function() {
// private variable
var name = “none";
// constructor
var cls = function(pname){name = pname;};
// parent class inheritance
cls.prototype = new BaseModel();
// public method
cls.prototype.func1 = function() {…};
return cls;
} () );
Actually、the
Javascript structure
has their own OOP
template, which I
don’t like.
Cut paste to script library
Xpages call から呼び出し
Output Result
Storing the new object
using viewScope
Some notes when using OOP in XPages
Previously, we have stored the new object into viewScope. In order to use objects persistently, choose “Save the page in
memory” at the XSP property setting
What do you think so far?
In order to better understand Object-oriented in
Javascript, I think it is best to try various concept
I prepared an HTML file for you to test 
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
// <![CDATA[
var FroutModel = (function() {
// private variable
var name = "hoge";
// コンストラクタ
var cls = function(pname){name = pname;};
// メソッド
cls.prototype.callMe = function() {alert("This is "+name);};
return cls;
}());
var frt = new FroutModel("Banana");
frt.callMe();
alert("frout.name="+frt.name);
// ]]>
</script>
</head>
<body>
</body>
</html>
Note that we are no longer afraid of Dojo
and jQuery
R
e
f
e
r
e
n
c
e
s
• 最強オブジェクト指向言語 JavaScript 再入門
Yuji Nojima slides. Developer at Foreignkey, Co. Ltd.
http://www.slideshare.net/yuka2py/javascript-23768378
• ブログ XPagesで行こう!
Kenji Ebihara’s blog. IBM Champion. Ricoh IT Solution
https://www.ibm.com/developerworks/community/blogs/ebi/
• MDN > 開発者向けのWeb技術 > Javascript
Mozilla Developer Network Javascript Portal
https://developer.mozilla.org/ja/docs/Web/JavaScript
• JSLint
To check Javascript bad coding practice
• http://www.jslint.com/
[A 3]Javascript oop for xpages developers - public
www.ktrick.com
This document are offered under the Creative Commons Attribution 2.1 Japan

More Related Content

KEY
JavaScript Growing Up
David Padbury
 
PDF
JavaScript Patterns
Stoyan Stefanov
 
PPTX
5 Tips for Better JavaScript
Todd Anglin
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
PDF
Prototype
Aditya Gaur
 
PDF
JavaScript - From Birth To Closure
Robert Nyman
 
PPTX
Javascript best practices
Manav Gupta
 
PDF
Javascript Design Patterns
Subramanyan Murali
 
JavaScript Growing Up
David Padbury
 
JavaScript Patterns
Stoyan Stefanov
 
5 Tips for Better JavaScript
Todd Anglin
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Prototype
Aditya Gaur
 
JavaScript - From Birth To Closure
Robert Nyman
 
Javascript best practices
Manav Gupta
 
Javascript Design Patterns
Subramanyan Murali
 

What's hot (20)

PDF
Java Script Best Practices
Enrique Juan de Dios
 
PPT
Advanced Javascript
Adieu
 
PPT
Object Oriented JavaScript
Donald Sipe
 
PPT
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
PPTX
Javascript Prototype Visualized
军 沈
 
PPTX
LinkedIn TBC JavaScript 100: Intro
Adam Crabtree
 
PDF
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
PDF
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
ODP
Javascript
theacadian
 
PDF
JavaScript Design Patterns
Derek Brown
 
PDF
Patterns for JVM languages - Geecon 2014
Jaroslaw Palka
 
ZIP
Fundamental JavaScript [In Control 2009]
Aaron Gustafson
 
PPTX
Javascript Common Design Patterns
Pham Huy Tung
 
PDF
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
PPTX
Understanding Object Oriented Javascript - Coffee@DBG June
Deepu S Nath
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
PDF
Secrets of JavaScript Libraries
jeresig
 
PDF
Scalable JavaScript Design Patterns
Addy Osmani
 
KEY
Javascript tid-bits
David Atchley
 
Java Script Best Practices
Enrique Juan de Dios
 
Advanced Javascript
Adieu
 
Object Oriented JavaScript
Donald Sipe
 
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
Javascript Prototype Visualized
军 沈
 
LinkedIn TBC JavaScript 100: Intro
Adam Crabtree
 
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Javascript basics for automation testing
Vikas Thange
 
Javascript
theacadian
 
JavaScript Design Patterns
Derek Brown
 
Patterns for JVM languages - Geecon 2014
Jaroslaw Palka
 
Fundamental JavaScript [In Control 2009]
Aaron Gustafson
 
Javascript Common Design Patterns
Pham Huy Tung
 
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Understanding Object Oriented Javascript - Coffee@DBG June
Deepu S Nath
 
Advanced JavaScript
Stoyan Stefanov
 
Secrets of JavaScript Libraries
jeresig
 
Scalable JavaScript Design Patterns
Addy Osmani
 
Javascript tid-bits
David Atchley
 
Ad

Similar to [A 3]Javascript oop for xpages developers - public (20)

PDF
JavaScript Essentials
Triphon Statkov
 
PDF
Prototype 120102020133-phpapp02
plutoone TestTwo
 
PPT
JavaScript In Object Oriented Way
Borey Lim
 
PPT
Javascript Object Oriented Programming
Bunlong Van
 
PDF
JS Level Up: Prototypes
Vernon Kesner
 
PPTX
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
PPTX
4front en
Vitaly Hornik
 
PPT
Advanced JavaScript
Fu Cheng
 
PPTX
JavaScript (without DOM)
Piyush Katariya
 
PDF
The many facets of code reuse in JavaScript
Leonardo Borges
 
PDF
Steam Learn: Javascript and OOP
inovia
 
KEY
2012 oct-12 - java script inheritance
pedro.carvalho
 
PDF
A class action
Luciano Colosio
 
PPTX
Ajaxworld
deannalagason
 
PDF
JavaScript Prototype and Module Pattern
Narendra Sisodiya
 
PPTX
JavsScript OOP
LearningTech
 
PDF
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PDF
Object-oriented Javascript
Daniel Ku
 
PDF
JavaScript Survival Guide
Giordano Scalzo
 
JavaScript Essentials
Triphon Statkov
 
Prototype 120102020133-phpapp02
plutoone TestTwo
 
JavaScript In Object Oriented Way
Borey Lim
 
Javascript Object Oriented Programming
Bunlong Van
 
JS Level Up: Prototypes
Vernon Kesner
 
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
4front en
Vitaly Hornik
 
Advanced JavaScript
Fu Cheng
 
JavaScript (without DOM)
Piyush Katariya
 
The many facets of code reuse in JavaScript
Leonardo Borges
 
Steam Learn: Javascript and OOP
inovia
 
2012 oct-12 - java script inheritance
pedro.carvalho
 
A class action
Luciano Colosio
 
Ajaxworld
deannalagason
 
JavaScript Prototype and Module Pattern
Narendra Sisodiya
 
JavsScript OOP
LearningTech
 
OOPS JavaScript Interview Questions PDF By ScholarHat
Scholarhat
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Object-oriented Javascript
Daniel Ku
 
JavaScript Survival Guide
Giordano Scalzo
 
Ad

More from Kazunori Tatsuki (8)

PDF
「dominoワークフローはここまで進化した!v12クラウドネイティブがもたらす新たなアプリ運用」ケートリックウェビナー2021.06.25
Kazunori Tatsuki
 
PPTX
Xpagesからさらにその先へ、最新Dominoアプリケーション開発で 企業のノーツアプリはこう生まれ変わる
Kazunori Tatsuki
 
PPTX
XPagesDay 2016 「xpagesでjava開発するぞ!」
Kazunori Tatsuki
 
PPTX
20151118パートナーソリューションセミナー2015プレゼンテーション public
Kazunori Tatsuki
 
PPTX
IBM Connect Japan 2016 「ドミノアプリをカンタンWEB化!業務アプリ作成ツール 「Aveedo」のご紹介」
Kazunori Tatsuki
 
PDF
20150225 テクテクlotus技術者夜会 ibm connect ed2015フィードバック 公開用
Kazunori Tatsuki
 
PPT
[A 3]SSJSでも使える!Javascriptでオブジェクト指向プログラミング入門
Kazunori Tatsuki
 
PDF
【C-3】ジャンボフェリー 予約システムの事例からみるXPagesを使った提案・開発の概要
Kazunori Tatsuki
 
「dominoワークフローはここまで進化した!v12クラウドネイティブがもたらす新たなアプリ運用」ケートリックウェビナー2021.06.25
Kazunori Tatsuki
 
Xpagesからさらにその先へ、最新Dominoアプリケーション開発で 企業のノーツアプリはこう生まれ変わる
Kazunori Tatsuki
 
XPagesDay 2016 「xpagesでjava開発するぞ!」
Kazunori Tatsuki
 
20151118パートナーソリューションセミナー2015プレゼンテーション public
Kazunori Tatsuki
 
IBM Connect Japan 2016 「ドミノアプリをカンタンWEB化!業務アプリ作成ツール 「Aveedo」のご紹介」
Kazunori Tatsuki
 
20150225 テクテクlotus技術者夜会 ibm connect ed2015フィードバック 公開用
Kazunori Tatsuki
 
[A 3]SSJSでも使える!Javascriptでオブジェクト指向プログラミング入門
Kazunori Tatsuki
 
【C-3】ジャンボフェリー 予約システムの事例からみるXPagesを使った提案・開発の概要
Kazunori Tatsuki
 

Recently uploaded (20)

PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PPTX
The Power of IoT Sensor Integration in Smart Infrastructure and Automation.pptx
Rejig Digital
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Doc9.....................................
SofiaCollazos
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
The Power of IoT Sensor Integration in Smart Infrastructure and Automation.pptx
Rejig Digital
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Software Development Company | KodekX
KodekX
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 

[A 3]Javascript oop for xpages developers - public

  • 1. www.ktrick.com A-3 Javascript OOP for XPages Developers Kazunori Tatsuki @ Ktrick Co,. Ltd.
  • 2. • Name: Kazunori Tatsuki • Company: KTrick Co,. Ltd. • Title: Owner of KTrick Co,. Ltd. • My Experience: - 7 years development experience as IBM Vender in US. - XPages and all other domino related languages, C, C++, Objective-C, Java, PHP etc. Introduction
  • 3. Are you struggling with developing XPages application? Why?
  • 4. Javascript? Client side Javascript… Sever side Javascript… What’s different?
  • 5. Nowadays Javascript sample code is like this … var func1 = function(){ alert("hello!"); } var myButton = new Button({ label: "Show me!", onClick: function(){ myDialog.set("content", “Hello!"); myDialog.show(); } }, "progbutton").startup();
  • 6. Javascript used to be like this… function func1(param){ alert( param); } It is easier to understand, isn’t it? Why has it changed?
  • 7. Have you ever seen this? ( function() { //contents here… }() ); Hmmm, Too much parenthesis… Actually these are all different meaning.
  • 9. In this session, I would like you to understand the structure of Javascript by breaking down the source code one by one.
  • 10. Object Oriented Programming What’s the benefits of using OOP?
  • 11. Benefits of OOP ・Reusability ・ Refactoring ・ Extensibility ・ Maintenance ・ Efficiency As a result, you can reduce the time and cost for development and testing.
  • 12. OOP What do you imagine from this word?
  • 14. If you already have OOP experience, you may think of 「 Class 」
  • 15. Is there class in Javascript?
  • 16. Nope
  • 17. How can we develop OOP without class?
  • 20. To begin with, Javascript only has Object.
  • 21. sub Class (type) By the way, Class is like this… Super Class (type) Object (Instance) Object (Instance) Object (Instance) NEW NEW NEW Class is like ‘type’. You define the class as the abstract type for instance. Then you can generate the instances from the classes. By doing this, you can generate same type objects (instances) easily ,which have the same functions and attributes.
  • 22. Again, Javascript doesn’t have Class structure… then…
  • 23. Javascript Prototype chain is like this… Object (Instance) Object (Instance) Object (Instance) Object (Instance) Dele gate Dele gate Dele gate You can connect the objects and objects like chain. Then if the original object doesn’t have the specific methods or variables, then Javascript delegates to search in the other object until they are found. Object (Instance) Dele gate
  • 24. Let’s take a look at the actual source code. ※Basically please consider that the sample codes are written in client side Javascript because server side Javascript cannot use some methods like alert(). However the theories and structures are the same!
  • 26. How to generate Object? var obj = { }; That’s it. An empty object was generated.
  • 27. Object structure var obj = { key: value }; Object can have Properties (the set of Key and value) That’s it! var obj = { name : “Apple”, size : 500 }; alert(obj.name); (i.e.)
  • 28. var obj = { func1 : function( ) {alert(“hoge”); } }; obj.func1(); // Output is “hoge” In Javascript, even the method is a “value” var func = function( ){}; A method is a value, therefore you can set it to a variable. Of course, you can operate variables as usual. (i.e) You can set the method to object property.
  • 29. Finally, explanation of Prototype Chain !! It’s THE Javascript OOP
  • 30. There are two important keywords below when learning Javascript Prototype Chain. ➲ __proto__ ➲prototype It’s very important to understand what is different from each other to understand Javascript OOP.
  • 32. __proto__ ・[Rule] All objects in Javascript have [[Prototype]] internally. However some browser application like Chrome, Firefox etc allow you to access the [[Prototype]] by using __proto__ property instead. Internal means that you basically cannot access to the [[Prototype]]
  • 33. Like this… Object (Instance) Object (Instance) Object (Instance) Object (Instance) Dele gate Dele gate Dele gate __proto__ __proto__ __proto__ __proto__
  • 34. Sample code (This sample code works in Chrome & Firefox since allowing you to access by using “__proto__” ) However this code alert “It is Apple” var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Object is empty…
  • 35. Confirm the order of source code(1) It alerts “It is Apple” Generates object named obj. It has name and callMe property. Generates object named objApple. name property is ”Apple”. Generates object named objBlackbox. There is no property. var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe();
  • 36. Confirm the order of source code (2) Set “obj” into objApple.__proto_ var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Generate object named obj. It has name and callMe property. Generate object named objApple. name property is ”Apple”. Generate object named objBlackbox. There is no property.
  • 37. Set “objApple” int objBlackbox.__proto__ var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Set “obj” into objApple.__proto_ Generate object named obj. It has name and callMe property. Generate object named objApple. name property is ”Apple”. Generate object named objBlackbox. There is no property. Confirm the order of source code (3)
  • 38. Confirm the method call (1) var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe();
  • 39. (1) There is no callMe() method in objBlackbox var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Confirm the method call (2)
  • 40. (2)Search object stored in __proto__ (objApple) var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Confirm the method call (3) (1) There is no callMe() method in objBlackbox
  • 41. (3) There is no callMe() method in objApple neither. var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); (2)Search object stored in __proto__ (objApple) (1) There is no callMe() method in objBlackbox Confirm the method call (4)
  • 42. (4) Serach object stored in __proto__ (obj) var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Confirm the method call (5) (3) There is no callMe() method in objApple neither. (2)Search object stored in __proto__ (objApple) (1) There is no callMe() method in objBlackbox
  • 43. (5) Found callMe() method in obj!var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); (4) Serach object stored in __proto__ (obj) Confirm the method call (6) (3) There is no callMe() method in objApple neither. (2)Search object stored in __proto__ (objApple) (1) There is no callMe() method in objBlackbox
  • 44. (1) callMe() method is called. var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Confirm the attribute call (1)
  • 45. (2) Refer “name” property ※”this” means “object itself.” var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); Confirm the attribute call (2) (1) callMe() method is called.
  • 46. (3) There is no “name” property in objBlackbox var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); (2) Refer “name” property ※”this” means “object itself.” Confirm the attribute call (3) (1) callMe() method is called.
  • 47. (4) Search the object stored in __proto__ (objApple) var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); (3) There is no “name” property in objBlackbox (2) Refer “name” property ※”this” means “object itself.” Confirm the attribute call (4) (1) callMe() method is called.
  • 48. (5) Found ”name“ in objApple ! var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); (4) Search the object stored in __proto__ (objApple) (3) There is no “name” property in objBlackbox (2) Refer “name” property ※”this” means “object itself.” Confirm the attribute call (5) (1) callMe() method is called.
  • 49. (6) There is “name” property in obj as well. However not referred var obj = { name : “none”, callMe : function() { alert(“It is ” + this.name); } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj; var objBlackbox = { }; objBlackbox.__proto__ = objApple; objBlackbox.callMe(); (5) Found ”name“ in objApple ! (4) Search the object stored in __proto__ (objApple) (3) There is no “name” property in objBlackbox (2) Refer “name” property ※”this” means “object itself.” Confirm the attribute call (6) (1) callMe() method is called.
  • 50. Like in the sample code, if the original object does not have the methods or attributes, then Javascript delegates to the chained object to search them via __proto__ until found This is Prototype Chain ! Object (Instance) Object (Instance) Object (Instance) Object (Instance) Dele gate Dele gate Dele gate Not Found Found Not Found Not Found
  • 51. Now is it ok to understand __proto__ ?
  • 52. Wait a minutes… __proto__ (aka [[Prototype]]) is just internal property. Then we should not use the code like below... objApple.__proto__ = obj; Is it true?
  • 53. Yes, Do not use it!
  • 54. In fact I used the code like below as an example so that I can explain the internal mechanism more clearly. However, there is an alternative way to achieve the same logic. var objApple = { name : “Apple”}; objApple.__proto__ = obj;
  • 55. The answer is using “New” operation (i.e.)This is the sample code equivalent to the previous sample. var obj = new Frout(“Apple”); Breakdown of this code later on. var Frout = function( pname ) { this.name = pname; } Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”);
  • 56. ➲ prototype Before moving forward, there is an another keyword called ”prototype” It’s very important to understand what is different from __proto__!
  • 57. prototype ・[Rule] All function objects have prototype property. “function object” Hmm, New keyword again... But don’t worry. You already know this.
  • 58. Function Object is like this… It was already explained in previous slide. Function(method) can be value. var func = function( ){};
  • 59. All Function Objects have prototype property. Again, For example, var func = function( ){}; alert( func.prototype ); The second line above does not output “undefined”. This means prototype exists as the default. However after defining the function object, the prototype refer just empty object. Then, How to use prototype?
  • 60. The answer is… Prototype is used with new operation when the object is generated!
  • 61. Confirm by source code (This is the sample code equivalent to previous sample for __proto__) Define Constructor Generates Function Object. name is undefined at this moment. Set the method to the prototype property of Frout object. Generate objApple object by using new operation. By the prototype chain, objApple can call the callMe() method of Frout prototype var Frout = function( pname ) { this.name = pname; } Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”); objApple.callMe(); Alert 「It is Apple」 ※ it is not “objApple.prototype.callMe() “ Explain in the next slide
  • 62. var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; Try to breakdown the process of new operation artificially. var objApple = new Frout(“Apple”);
  • 63. var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; (1) Generates object. var objApple = new Frout(“Apple”); Try to breakdown the process of new operation artificially.
  • 64. var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; (2) Set prototype property into __proto__. Frout.prototype Has callMe() method. Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”); Try to breakdown the process of new operation artificially. (1) Generate object.
  • 65. var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; (3) Execute constructor function. Constructor function is executed only when function object is generated. Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”); Try to breakdown the process of new operation artificially. (2) Set prototype property into __proto__. Frout.prototype Has callMe() method. (1) Generate object.
  • 66. var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; (4) Returns the object. Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”); Try to breakdown the process of new operation artificially. (3) Execute constructor function. Constructor function is executed only when function object is generated. (2) Set prototype property into __proto__. Frout.prototype Has callMe() method. (1) Generate object.
  • 67. var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”); var objApple = new Frout(“Apple”); Try to breakdown the process of new operation artificially. (4) Return the object. (3) Execute constructor function. Constructor function is executed only when function object is generated. (2) Set prototype property into __proto__. Frout.prototype Has callMe() method. (1) Generate object.
  • 68. The sample code using for explanation of __proto__ var obj = {}; obj.__proto__ = Frout.prototype; Frout.apply(obj, arguments ); return obj; The breakdown process of new operation artificially. If you replace obj to Frout.prototype, then both objects have callMe() Frout.prototype.callMe = function() {…}; var obj = { name : “none”, callMe : function() { … } }; var objApple = { name : “Apple”}; objApple.__proto__ = obj;
  • 69. Difference between __proto__ and prototype Prototype is used when object is generated by new operation to be stored in __proto__. Used by prototype search mechanism internally. By the new operation, object prototype is automatically stored. __proto__ prototype To make a prototype chain, use this prototype. __proto__ is just needed to understand the internal mechanism. Use new and prototype for OOP.
  • 70. So, a little Brain Teaser
  • 71. Frout object preparation and if we describe We don’t want to write this, correct? It is not useful var Frout = function( name ) { this.name = name; } Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var Frout = function( name ) { this.name = name; this.callMe = function() { alert(“It is ” + this.name); }; }
  • 72. From an OOP perspective this is bad But let’s continue…
  • 73. The reason: var Frout = function( name ) { this.name = name; this.callMe = function() { alert(“It is ” + this.name); }; } In this code, without a prototype, it will be defined inside the constructor In other words: Everytime new is called to create this object, callMe is also instantiated. In Contrast, methods defined in the prototype, falls within the prototype chain inconsistently Memory efficiency is better because the mechanism allows reference to the same memory when new objects are created. Frout.prototype.callMe = function() { alert(“It is ” + this.name); };
  • 75. From the sample code earlier: This function of brackets is integral to what kind of work? ( function() { //code }() );
  • 76. Arriving at the conclusion Calling an anonymous function immediately We are going to do this
  • 77. Immediately after the function object is returned, we're calling the function. Create function Object。 ( function() { //code・・・ }() ); Anonymous funtion with no variable This was done through a very complicated process。 This is used as such that when the page is loaded, it processes only once。 There are 2 parts? //Immediate function1 (function () { //code・・・ }()); //Immediate function2 (function () { //code・・・ })(); Both are immediate function. In JSLint(http://www.jslint.com/) the former code seems to be preferred
  • 79. the result… var Frout = function( name ) { this.name = name; } Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; var objApple = new Frout(“Apple”); alert( objApple.name ); You should not be able see if it was private variable Is this a private variable? protected variable? public variable? To be considered OOP, don’t we want to hide the private variable?
  • 80. (Naturally)we are able to see orz but wait a minute…
  • 81. I modified something like this var FroutModel = (function() { // private variable var name = “none"; // Constructor var cls = function(pname){name = pname;}; // public method cls.prototype.callMe = function() {alert("This is "+name);}; return cls; } () ); var Frout = function( name ) { this.name = name; } Frout.prototype.callMe = function() { alert(“It is ” + this.name); }; Before re edit
  • 82. var FroutModel = (function() { // private variable var name = “none"; // Constructor var cls = function(pname){name = pname;}; // public method cls.prototype.callMe = function() {alert("This is "+name);}; return cls; } () ); var objApple = new FroutModel(“Apple”); alert( objApple.name ); objApple.callMe(); To make sure that undefined is hidden Can be successfully called
  • 83. Somehow it very much looks like a class
  • 84. In addition to class inheritance, you can implement like this: var FroutModel = (function() { // private variable var name = “none"; // Constructor var cls = function(pname){name = pname;}; // Parent class inheritance cls.prototype = new BaseModel(); // public method cls.prototype.callMe = function() {alert("This is "+name);}; return cls; } () ); Like this: BaseModel object and FroutModel object are doing the same implementation
  • 85. Below are some descriptions about the code up to now. Please challenge yourself by solving the puzzle var FroutModel = (function() { // private variable var name = “none"; // constructor var cls = function(pname){name = pname;}; // parent class inheritance cls.prototype = new BaseModel(); // public method cls.prototype.callMe = function() {alert("This is "+name);}; return cls; } () ); Hint: Scope chain cannot be seen from the outside Hint:Generate another object inside the FroutModel function Hint: Returns the object that was generated in the FroutModel Hint: Immediate function
  • 86. This time、 XPages discussion has not even come out yet…
  • 87. Now, Javascript has two types: Client-side Javascript Server-side Javascript Both have the concept of class
  • 88. Lets try the last class’s implementation of SSJS. It’s also useful to copy to script lib var AppModel = (function() { // private variable var name = “none"; // constructor var cls = function(pname){name = pname;}; // parent class inheritance cls.prototype = new BaseModel(); // public method cls.prototype.func1 = function() {…}; return cls; } () ); Actually、the Javascript structure has their own OOP template, which I don’t like.
  • 89. Cut paste to script library
  • 90. Xpages call から呼び出し Output Result Storing the new object using viewScope
  • 91. Some notes when using OOP in XPages Previously, we have stored the new object into viewScope. In order to use objects persistently, choose “Save the page in memory” at the XSP property setting
  • 92. What do you think so far? In order to better understand Object-oriented in Javascript, I think it is best to try various concept I prepared an HTML file for you to test  <!DOCTYPE html> <html> <head> <script type="text/javascript"> // <![CDATA[ var FroutModel = (function() { // private variable var name = "hoge"; // コンストラクタ var cls = function(pname){name = pname;}; // メソッド cls.prototype.callMe = function() {alert("This is "+name);}; return cls; }()); var frt = new FroutModel("Banana"); frt.callMe(); alert("frout.name="+frt.name); // ]]> </script> </head> <body> </body> </html>
  • 93. Note that we are no longer afraid of Dojo and jQuery
  • 94. R e f e r e n c e s • 最強オブジェクト指向言語 JavaScript 再入門 Yuji Nojima slides. Developer at Foreignkey, Co. Ltd. http://www.slideshare.net/yuka2py/javascript-23768378 • ブログ XPagesで行こう! Kenji Ebihara’s blog. IBM Champion. Ricoh IT Solution https://www.ibm.com/developerworks/community/blogs/ebi/ • MDN > 開発者向けのWeb技術 > Javascript Mozilla Developer Network Javascript Portal https://developer.mozilla.org/ja/docs/Web/JavaScript • JSLint To check Javascript bad coding practice • http://www.jslint.com/
  • 96. www.ktrick.com This document are offered under the Creative Commons Attribution 2.1 Japan