193 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
Calling an external service from your chat bot
Mon, Mar 20th 2017 49
NWCJS Meetup: Creating Bots with JavaScript and the Microsoft Bot Framework
Tue, Mar 14th 2017 17
Setting up the sample Azure bot to work locally with the bot emulator
Mon, Mar 13th 2017 9
Adding your bot code to VSTS for source control and configuring continuous integration
Mon, Mar 6th 2017 10
Adding your Azure bot framework bot into Skype
Tue, Feb 28th 2017 9
How to create a sample nodejs bot which utilizes Azure Functions in 15 minutes
Thu, Feb 23rd 2017 7
NWCJS meetup – Angular 2 for beginners
Fri, Feb 10th 2017 10
Top 10
Calling an external service from your chat bot
Mon, Mar 20th 2017 49
NWCJS Meetup: Creating Bots with JavaScript and the Microsoft Bot Framework
Tue, Mar 14th 2017 17
Adding your Let’s Encrypt SSL certificate into your Domino keyring file
Thu, Feb 18th 2016 14
One way to make a responsive XPages viewPanel
Tue, Nov 11th 2014 12
Angular.js in XPages #3 – The first app
Sun, May 11th 2014 11
WebSockets comes to XPages!! Awesome – Awesome – Awesome
Tue, May 20th 2014 11
Accessing the original element when using Select2-focus
Sun, Aug 17th 2014 10
THANK YOU NOTES IN 9 !!!!!!!!!!!!!
Mon, Sep 19th 2016 10
NWCJS meetup – Angular 2 for beginners
Fri, Feb 10th 2017 10
Adding your bot code to VSTS for source control and configuring continuous integration
Mon, Mar 6th 2017 10


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
7 hits



Recent Blog Posts
49
Calling an external service from your chat bot
Mon, Mar 20th 2017 1:49a   Mark Roden
In this article I will show to how integrate simple commands (intents) into your bot to then integrate with an external service. Introduction In previous articles we have looked at how to create a sample Azure bot and in this article we will be looking into how “intents” work. The microsoft documentation on dialogIntents can be found here (for the … Continue reading Calling an external service from your chat bot
17
NWCJS Meetup: Creating Bots with JavaScript and the Microsoft Bot Framework
Tue, Mar 14th 2017 2:33a   Mark Roden
Next week’s Northwest Chicago JavaScript meetup (23 March @PSCGroup) features our own Adam Lepley speaking about some of the work he has been doing on the Microsoft Bot Framework. https://www.meetup.com/Northwest-Chicago-JavaScript/events/236993059/ “Bots, they are invading our chat apps, now it’s time to learn to develop your own. Come learn how Microsoft’s Bot Framework enables you to leverage … Continue reading NWCJS Meetup: Creating Bots with JavaScript and the Microsoft Bot&#
9
Setting up the sample Azure bot to work locally with the bot emulator
Mon, Mar 13th 2017 2:27a   Mark Roden
In this article I will demonstrate how to configure your local development environment to work with the environmental variables set up within your Azure environment in the sample bot previously discussed, Introduction  In the previous article we looked at how to create a sample azure bot and then how to configure it in VSTS for continuous integration. If you … Continue reading Setting up the sample Azure bot to work locally with the bot emulator
10
Adding your bot code to VSTS for source control and configuring continuous integration
Mon, Mar 6th 2017 3:00a   Mark Roden
A
9
Adding your Azure bot framework bot into Skype
Tue, Feb 28th 2017 2:30a   Mark Roden
A
7
How to create a sample nodejs bot which utilizes Azure Functions in 15 minutes
Thu, Feb 23rd 2017 2:30a   Mark Roden
In this article I will demonstrate how to create a nodejs bot hosted in Azure. The bot will be created from a sample provided from Microsoft. The example bot will use Azure Functions as the trigger for it’s communication. You will need a Microsoft Azure account to be able to follow this process for yourself. This is … Continue reading How to create a sample nodejs bot which utilizes Azure Functions in 15 minutes
10
NWCJS meetup – Angular 2 for beginners
Fri, Feb 10th 2017 1:24a   Mark Roden
A
4
What do Apple Airpods and Spongebob Squarepants have in common?
Mon, Feb 6th 2017 4:30a   Mark Roden
So in an uncommonly, just for the hell of it, post I wanted to share something techie but just for the sake of amusement. When my family was looking at the new AirPods online we noticed something rather amusing……for those of you who don’t watch Spongebob Squarepants you won’t get this, but we are talking … Continue reading What do Apple Airpods and Spongebob Squarepants have in common?
5
Five years of blogging and personal growth
Tue, Jan 24th 2017 1:20p   Mark Roden
A
6
Azure Machine Learning Studio Office Add-In
Wed, Jan 18th 2017 12:45a   Mark Roden
I am currently in the process of learning more about Azure Machine Learning Studio . Within there you can create and train predictive models. As I am going through some of the examples I came across a great example of an Office Add-In. I am creating a simple weather predicting example based on data I downloaded from … Continue reading Azure Machine Learning Studio Office Add-In




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