191 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
Predictive Analysis and Machine Learning
Tue, Jan 10th 2017 60
19 Jan – Speaking at Northwest Chicago JavaScript meetup – Office.js
Wed, Jan 4th 2017 20
Building Office Add-ins using Office.js – the book, the website
Thu, Dec 15th 2016 5
Sharing data between Office Add-Ins using localStorage
Mon, Nov 21st 2016 5
Using “F12” in Windows to get an Office Add-In development developer console
Mon, Nov 14th 2016 4
Thank you – Microsoft MVP Summit !!!
Thu, Nov 10th 2016 5
SharePoint now accessible via the Microsoft Graph beta
Mon, Oct 3rd 2016 4
Top 10
Predictive Analysis and Machine Learning
Tue, Jan 10th 2017 60
19 Jan – Speaking at Northwest Chicago JavaScript meetup – Office.js
Wed, Jan 4th 2017 20
Adding your Let’s Encrypt SSL certificate into your Domino keyring file
Thu, Feb 18th 2016 10
Using jQuery to remove Dojo CSS from an XPage
Thu, Feb 20th 2014 6
Angular.js in XPages #1 – Using the right IDE for development
Sun, Apr 27th 2014 6
Accessing the original element when using Select2-focus
Sun, Aug 17th 2014 5
Creating an XPages Websockets chat client using Bluemix
Sun, Nov 9th 2014 5
Thank you – Microsoft MVP Summit !!!
Thu, Nov 10th 2016 5
Sharing data between Office Add-Ins using localStorage
Mon, Nov 21st 2016 5
Building Office Add-ins using Office.js – the book, the website
Thu, Dec 15th 2016 5


Prototypal inheritance of SSJS across the whole server in XPages
Twitter Google+ Facebook LinkedIn Addthis Email Gmail Flipboard Reddit Tumblr WhatsApp StumbleUpon Yammer Evernote Delicious
mark roden    

A friend asked me the other day how would I determine where a string lay within a JavaScript Array. I of course answered – “it depends! If you are using newer IE or a non-crappy implementation of the JavaScript engine then use Array.indexOf – it has been around for years…..”

var arr = ["simon", "marky", "david"]
return arr.indexOf("marky") // 1.0

Well – turns out the friend was asking about SSJS in XPages and it turns out that apparently XPages has one of those crappy JavaScript engines of which I spoke and Array.indexOf is not supported (facepalm). (*really??*)

<xp:text escape="true" id="computedField1">
	<xp:this.value><![CDATA[#{javascript:
		var arr = ["simon", "marky", "david"]
		return arr.indexOf("marky")
	}]]></xp:this.value>
</xp:text>

arr1

Polyfill

OK so there is a simple polyfill for the indexOf, which has also been around for years as this article written in 2007 demonstrates…..

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

And if we add this to our XPages sample code we get the answer we are expecting….

<xp:text escape="true" id="computedField1">
	<xp:this.value><![CDATA[#{javascript:
		if(!Array.indexOf){
		    Array.prototype.indexOf = function(obj){
		        for(var i=0; i<this.length; i++){
		            if(this[i]==obj){
		                return i;
		            }
		        }
		        return -1;
		    }
		}

		var arr = ["simon", "marky", "david"]
		return arr.indexOf("marky")
	}]]></xp:this.value>
</xp:text>

arr2

 

Yay !

Prototyping in JavaScript

For a better explanation that I can give, check out this StackOverflow answer on what is prototyping – suffice to say that every JavaScript object has a prototype property and this is the basis for prototypal (as opposed to class based) inheritance in the JavaScript language.

What we did in the polyfill above is: we simply said if Array.indexOf method does not exist, then we created one.

So what does this have to do with XPages?

In JavaScript when you prototype a function, the scope is on the page you are looking at. As soon as you leave the page the prototype is gone. In XPages it seems to be a little broader than that…..

I (unintentionally) discovered that once you prototype the Array.indexOf, not only does it become available on the same page, it is permanently on the same page. You run the prototype code once….then take it away, and it still works. The scope would appear to be at the application level…..

Well so then I asked some people who are way smarter than I and Nathan Freeman pointed me to his article about XPages Performance Pro Tips and told me he expected that the prototype would actually be at the server level and he was right….

I included the URL of the test database in the picture above to demonstrate that when I move the simple code to another database – it still works (after the prototype code has been run once)

rr3

And just to be sure – in another browser !!

arr4

 

Once the Array.prototype.indexOf was created, it became available in all applications on the server, instantly.

Killing the prototype

  • Restarting HTTP *DOES NOT* stop this.
  • Killing all browsers and starting again does not stop this

As far as I can tell you have modified the way the SSJS works on the server until you kill it. So far I have run a test over 24 hours later and it is still working……

This is COOL right?

Well for one it means that you can seriously enhance all your JavaScript on the server in ways which make custom libraries look like a waste of time. We could truly add all the corporate libraries to SSJS once when the server loads and they would always be available to all developers at all times.

arr5

 

And the even more awesome (depending on perspective…..)

arr7

 

Well yeah but…….

You can also serious screw up perfectly good code across the server

arr6

Conclusion

Prototypal inheritance is at the root of JavaScript programming language and is very powerful. What we can see here though is that we are able to affect code across the entire server with a few lines of code. This is *very* cool and *very scary* at the same time.

So with this knowledge comes great responsibility – use it at your own risk and make sure you have very good server side code review if you are going to go down this path.

 




---------------------
http://xomino.com/2014/03/02/prototypal-inheritance-of-ssjs-across-the-whole-server-in-xpages/
Mar 02, 2014
4 hits



Recent Blog Posts
60
Predictive Analysis and Machine Learning
Tue, Jan 10th 2017 1:42a   Mark Roden
A
20
19 Jan – Speaking at Northwest Chicago JavaScript meetup – Office.js
Wed, Jan 4th 2017 2:20p   Mark Roden
A
5
Building Office Add-ins using Office.js – the book, the website
Thu, Dec 15th 2016 2:30a   Mark Roden
I wanted to show some love for the new website and book by Michael Zlatkovsky. Michael is a Software Developer and Program Manager in the Microsoft Office Extensibility team. I met Michael at the MS MVP Summit back in October and we had a great conversation or two about Office Addins and their future development. So Michael’s … Continue reading Building Office Add-ins using Office.js – the book, the website
5
Sharing data between Office Add-Ins using localStorage
Mon, Nov 21st 2016 2:42a   Mark Roden
In this article I will show how the underlying dependence on the browser (in this case IE11 in Windows) allows us to pass data between Office Add-Ins through the use of HTML5 localStorage. At this point this is a theoretical post as I haven’t thought of a good use case yet, I am sure I … Continue reading Sharing data between Office Add-Ins using localStorage
4
Using “F12” in Windows to get an Office Add-In development developer console
Mon, Nov 14th 2016 1:00a   Mark Roden
A
5
Thank you – Microsoft MVP Summit !!!
Thu, Nov 10th 2016 6:55a   Mark Roden
A
4
SharePoint now accessible via the Microsoft Graph beta
Mon, Oct 3rd 2016 8:00p   Mark Roden
A
1
Microsoft MVP for Office Development
Mon, Oct 3rd 2016 8:34a   Mark Roden
This weekend I was thrilled to receive an email from Microsoft announcing that I had been recognized as a Microsoft Most Valuable Professional (MVP) in the Office Development technical community. This is a very natural evolution of my journey over the last two years since I branded out from nearly 20 years in IBM Domino. As I … Continue reading Microsoft MVP for Office Development
3
Speaking at Dreamforce next week :)
Wed, Sep 28th 2016 8:16a   Mark Roden
A
1
Thanks Notes in 9 blooper
Sun, Sep 25th 2016 9:34p   Mark Roden
Selfishly for my own amusement, entirely at Dave’s expense #thanksnotesin9




Created and Maintained by Yancy Lent - About - Planet Lotus Blog - Advertising - Mobile Edition