Tuesday, January 24, 2017

Javascript Objects - The Truth

After building a project that attempted to use an object oriented way with Javascript I found that the rules of prototyping in Javascript were very confusing.  After reading several articles that weren't really helping I found one that mentioned all the "rubbish" in articles he had read.  That led me to an experiment to get to the truth of the matter - code!
So here is the code.  Run this, observe its assumptions, and I think you will soon be on your way to really understanding Javascript objects, inheritance and prototypes.
<!doctype html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Object Test for Browsers</title>
<style type="text/css">
  body {
<script type="text/javascript">
Object.prototype.deepCopyValues = function() {
    // returns a deep copy of this without function memebers - quicker
    return JSON.parse(JSON.stringify(this));

Object.prototype.deepCopy = function() {
    // returns a deep copy of this
    function deepCopyFunctions(org, copy) {
        if (org == null) {
            return null;
        var keys = Object.keys(org);
        for (var i = 0; i < keys.length; i++) {
            var key = keys[i];
            var o = org[key];
            if (typeof(o) == 'function') {
                copy[key] = o;
            } else if (typeof(o) == 'object') {
                copy[key] = deepCopyFunctions(o, copy[key]);
        return copy;
    var copy = this.deepCopyValues();
    return deepCopyFunctions(this, copy);
function run() {
    var o = {
        a : 1,
        d : 'string'
    var o2 = o.deepCopy();
    var o3 = o;
    if (o !== o3) {
        return 'An object is equal to (===) a reference of itself';
    if (o === o2) {
        return 'An object is not equal to (!==) a deep copy of itself';
    if (o == o2) {
        return 'An object is not equal to (!=) a deep copy of itself';
    o.a = 2;
    if (o2.a == 2) {
        return 'Changing a member of an object does not affect a deep copy of itself';
    o3 = { a:5 };
    if (o.a == 5) {
        return 'Changing an object reference to a different object does not change the original object.';
    if (o.__proto__ != ({}).__proto__) {
        return 'An object that is not created with new has an empty prototype.';
    if (o2.__proto__ != ({}).__proto__) {
        return 'A deep copy of an object not created with new has a null prototype.';
    function fnConstructorA(){
        this.a = 1;
        this.d = 'string';
    try {
    } catch (e) {
        if (e.message != 'Cannot set property \'a\' of undefined' && // Chrome
            e.message != 'this is undefined') { // Firefox
            return 'Calling a proper constructor without new makes the \'this\' variable in the constructor undefined. message=' + e.message;
    var A = new fnConstructorA();
    var A2 = A.deepCopy();
    var A3 = A;
    if ((A.__proto__).__proto__ !== ({}).__proto__) {
        return 'A new created object who\'s constructor has no prototype has the default Object prototype.';
    if (A.constructor !== fnConstructorA) {
        return 'a new created object\'s constructor is the function that created the object.';
    if (A !== A3) {
        return 'An new created object is equal (===) to a reference of itself';
    if (A === A2) {
        return 'A new created object is not equal (===) to a copy of itself';
    if (A != A3) {
        return 'An new created object is equal (==) to a reference of itself';
    if (A == A2) {
        return 'A new created object is not equal (!=) to a copy of itself';
    function fConstructorB(o) {    // like Object.create()
        this.__proto__ = o;
        this.a = 5;
        this.data = 'that string';
    var B = new fConstructorB(A);
    if (B.__proto__ !== A) {
        return 'A proper Object.create() call yields an object with a prototype that equals the object given to the function.';
    if (B.a == A.a) {
        return 'Values in a child object overshadow the same value in its prototype.';
    if (B.d != 'string') {
        return 'Values in a prototype are visible from a child object if that value is not defined in the parent object.';
    B.a = 14;
    if (B.a != 14) {
        return 'A value in a child object can be changed.';
    if (A.a != 1) {
        return 'A value in a prototype of an object is not affected if a vaule in a child object is changed.';
    B.__proto__.e = 'new string';
    if (B.e != 'new string') {
        return 'Changing a prototype\'s value within a child object affects the child objects same value if not defined in the child object.';
    A.f = 'proto changed';
    var C = new fConstructorB(A);
    if (B.f != 'proto changed') {
        return 'Changing a prototype object value affects any child objects of it that don\'t have that value defined.';
    if (C.f != 'proto changed') {
        return 'Changing a prototype object value affects any child objects of it that don\'t have that value defined.';
    return 'SUCCESS!!!!';
function runTests() {
    var str = run();
    document.body.innerHTML = str;
    <body onload="runTests();">

Wednesday, July 6, 2016

2016 Lameotech Award

2016 Techno Dweeb Lameotech Award goes to...
The Nikon D5200

The first camera I have ever owned that does NOT charge the battery when attached via USB to your PC.  Leave it on, and your battery will go completely dead.  Forget your charger when you travel - You're out of luck dude!

Monday, May 16, 2016

I've just about had it with Windows

I live far out of town in rural Idaho so I get pretty lame satellite internet service.  I am used to things working slow but lately, ever since the Windows 10 drive, my browsing experience has gotten so bad as to begin to be unusable.  Checking email on Chrome via live.com is so bad that sometimes when I am responding to or composing an email, I have to wait for individual keystrokes to complete in the composition window.  I know windows uses .ASP technology for its live.com site but does it actually have to go clear to the server for each keystroke?
To determine the problem I decided to switch browsers to Firefox and Explorer both were moderately better but still very slow.  So my next step was to boot up on a Linux Mint CD and see how my browsing on live.com worked from that OS - massive difference!  Probably 5-10x faster on Linux with Firefox.
While trying to figure out what was wrong with my system I did a deep scan using Windows Defender which took well over 24 hours to accomplish and found no problems.  My system has a nice SSD drive with defragging software in place and plenty of RAM and disk space.
I suspect a chrome virus using script injection on my youtube.com pages which frequently freezes up and is nearly unusable but have not found anything that finds a problem.  Looking on the Chrome debugger I see tons of scripts from all over the map but it too will lock up and prevent me from further investigation.
I used to work with an old 8080 2MHZ single processor 64k 87-bit RAM system I built back in the 70s and it was much faster than my Dual Proc Intel 2.13GHZ processor with 2GB of RAM, 64 bit OS and windows - even when simply using a text editor to code on Windows with no internet useage!
Looking at Task Manager revealed at times Windows Update pegging the hard drive with IO doing some kind of background install that just slowed my system to an unusable crawl.  I wondered if it wasn't pre-installing Windows 10 for me in the background.
So basically, my very powerful PC is more controlled by advertisers and viruses and Microsoft than it is by me.  It is clearly using a minuscule percentage of CPU power to do what I want it to do with the lion's share devoted to under-the-cover ops I have no clue about and no control over (without writing a post-graduate CS dissertation in the process).  Is it spying on me?  It is reporting my every keystroke to someone?  Is there a root kit in there doing tons of stuff I can't even see?  Why should the keyboard respond so slowly yet Task Manager say the CPU is idle 98% of the time?
Linux boots incredibly fast, and runs much faster, even when loading form a single CD.  It clearly shows me how much I am being robbed by corporate America.
So as a last effort to give Windows one more chance I decided to start downloading Windows 10 - I've been supposedly doing that for the last 4 hours but the resource monitor shows absolutely no internet activity as the windows spinning wheel goes round and round telling me it is downloading windows.  As I write this I am accessing the internet at the same time no problem.
Must be a bug.
I am very close to making the painful transition to Linux and chucking the OS I helped write into the waste basket - forcing me to abandon programs I have used for years like MSN Money and Free Cell and start completely over learning a new system.  Maybe Wine is good enough on Linux now to make the transition easier.
I hate to waste the time but Windows has become so bad I can hardly read email or watch youtube or enjoy facebook.  Personal computing should not be this way and this isn't the vision I worked so hard on at Microsoft for 20 years.

Tuesday, March 1, 2016

A possible way out of promise hell

I recently have been getting into promises (ie: nodejs, javascript type promises - see the Promise/A+ spec.)
I was reading several articles on this beast and found an exceptionally helpful one in the post entitled "We have a problem with promises" here.
Something told me that this could be explained better.
I believe I have found one possible better explanation.

The confusing thing about promises isn't the syntax but the very important side effects of each function involved.  What a function returns or does effects a promise dramatically.  This is because promises work entirely by side effects.
If we make the types clear, the code becomes much more understandable I think.

So lets set up some hungarian-like conventions for naming our functions and parameters and return values:

Type Prefixes:
p_     a promise
pfn_   a function that returns a promise (a promise generator)
fn_    a function that does not return a value
cbfn_  a commonJS callback that takes (fn_err, fn_success)
apcbfn_ an asynchronous promise callback function that takes (fn_resolve, fn_reject).
v_     a value (not a promise or a function)
vfn_   a function that returns a non-promise, non-function value
tfn_   a function that may throw an error
afn_   an asynchronous function that does not take a cbfn_ (does not return and throws are lost).
ajsfn_ an asynchronous commonJS function that takes a cbfn_ function (does not return and throws are lost).
resolve - a function that is called when a promise is successfully resolved with the value of the promise. Whatever value it returns becomes the value of the promise that called it.
reject -  a function that is called when a promise fails to accomplish its purpose and is called with an error value. Its return value (if any) is ignored by the promise  that called it.
Combine these to say that the value or function may be any of the types specified (if you want to get complicated).
In the "We have a problem with promises" post the author gives a little quiz of code for the reader to understand:
doSomething().then(function () {
  return doSomethingElse();

doSomething().then(function () {


So lets look at the first one with type prefixes added for the cases we want to study.
pfn_doSomething().then(function pfn_resolve() {
  return pfn_doSomethingElse();
What's happening here has several possibilities depending on what each promise ends up doing.
First lets recall how we create a promise:
var p_new = new Promise(apcbfn(pfn_vfn_resolve, fn_reject));
This is really the mother of all promise generators and once this line is run apcbfn() is instantly called.
Realize that apcbfn() is an asynchronous function and thus will never return and any throws it may do will be lost because it runs in a different call stack - except that within a promise, those throws will be caught by a closure and result in a call to fn_reject.  The only way this promise can be rejected or resolved is for one of it's callback functions to be called by the asynchronous function or for it to throw and exception.  Note that if the apcbfn() never calls either of its callback functions and never throws an exception, the promise is powerless to ever be resolved or rejected.
Once the promise is resolved (ie vfn_resolve() is called - if that is the case) its .then() function is called.  If it is rejected, the .then() function is never called - but if the promise has a .catch() function - that will be called on rejection.
Another thing to remember is that promises bubble up.  That means that each .then() in a chain is actually a nesting of .then() functions with the result of the inner .then() being passed up the chain.
This is because a resolved promise holds a value which can be returned from its resolve function. This is how promises can be nested - but the resolve function MUST return a value for this to work.
pfn_doSomething().then(function fn_resolve() {
  return pfn_doSomethingElse();
So above we have a function that returns a promise (pfn_doSomething).  That function, once it creates the promise instantly starts running the promise's apcbfn() function which never returns but because it is inside a promise will have any exceptions caught and passed on to the promise's reject function and thus its .catch() function as well.
The .then() function of pfn_doSomething() will be called only if that promise resolves successfully.
pfn_doSomethingElse() is also a promise generator which is only called upon successful resolution of pfn_doSomething() and once it is called it's corresponding internal apcbfn() function is called - which never returns either but will eventually call its reject or resolve function.  IF the resolve function returns a value, then the pfn_doSomethingElse() get's that value and returns it via fn_resolve() to pfn_doSomething() which will inherit that value internally but not call its fn_resolve() function because it already did before calling the .then() function and promises only call their fn_resolve() functions once by contract.
The reason the quiz given in the "We have a problem with promises" post can be difficult is because we don't know the actual fn_resolve(), fn_reject() and internals of apcbfn() for all the promises mentioned.  In fact, in the syntax of the original quiz, we don't even know what kind of functions the doSomething... functions are.  Here I am assuming they are promise generators and note that explicitly with the prefixes but I also must assume that the apcbfn() functions don't throw errors and that the fn_resolve() functions return values and don't throw errors either.
I think if a newbe to promises like me uses these prefixes in their first coding attempts, things will be easier to follow.
Perhaps this could become a convention to help us all read promise code better.

Sunday, May 17, 2015

Windows is fading into history yet...

When I made a visit to some old Microsoft friends of mine a month or so ago, they told me forget Windows - it's history.  And it probably is.
Microsoft, the company that put its software on every desktop in the world shot itself in the foot just too many times.
I recently was in town trying to print up some postcards done in Mail Merge with Word.  The margins weren't quite right and Staples didn't have any workstations for me to fix it.  I had to take it to another place that fixed PCs and pay $27 to tweak it and have it converted to PDF format.  Why PDF?  The person at the computer place explained that Microsoft's many versions of Word were not compatible with themselves and thus PDF format became the preferred printing format.

Here's how I would save Windows if I were the VP in charge:

  1. Separate the Kernel from the rest and sell it as a rock solid separate product.
  2. Sell the GUI and Shell separately and create versions that work on the Kernel that work like NT2000, XP, Win7 and Win8 and let the user chose which shell they want to install.
  3. Make the shells open source and let others make whatever shells they want to for the OS.
  4. Let all non-essential services and features be installed optionally.
  5. Let the user chose between a standard stupid user configuration and a savvy user configuration that doesn't hamper the user from accessing and seeing everything as it really is.
  6. Create a smart interactive website that documents the registry and all other aspects of Windows with the ability for users to contribute and improve it - kinda like a wiki for windows.
  7. Improve setups to install, uninstall and move/relocate each feature or app.
  8. All services or other components that store state should be able to write their state to a file and read it back in - this allows easy state comparisons for research/troubleshooting and helps make components portable.
I don't know if this would save things or not but it would give users much more choices and power to effect changes for themselves instead of this monolithic mess that keeps changing on us.

2 C

Sunday, May 11, 2014

Gotta love it

I am/was a UI developer.  My work forces me to think about the end user all the time.  So here's another lame Microsoft example from Windows Live Mail which I just had to rant about.
Is this lame or what?
Which Server?  What Certificate?  What does CN mean to an end-user?  What was the passed in value?
This is just a lazy dialog where the programmer just didn't want to take the time to locate the pertinent information to tell the user.  I can appreciate that sometimes, when an error happens, it is so far away from where the data is that it can take some real work to get that information to the user - but without it, the UI is kinda useless.
I keep getting these and I use 5 accounts on WLM so it's been difficult for me to fathom what is really the problem.

Rant Done.

Saturday, March 15, 2014

All glyphs for UTF-8

I spent some time producing a better glyph page for the UTF-8 codepage - just for kicks.  Maybe somebody can use it.

Go Here