Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6355577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:47:34+00:00 2026-05-24T22:47:34+00:00

I found one interesting project on github which deals with pdf rendering in the

  • 0

I found one interesting project on github which deals with pdf rendering in the browser.

I tried to read the code because I’m interested in this topic but I realized that my javascript knowledge is poor (insuficient).

There are constructs like:

var Obj = (function() {
    function constructor(type, value) {
    this.type = type;
    this.value = value;
    }

    constructor.prototype = {
    };

    var types = [
    "Bool", "Int", "Real", "String", "Name", "Null",
    "Array", "Dict", "Stream", "Ref",
    "Cmd", "Error", "EOF", "None"
    ];

    for (var i = 0; i < types.length; ++i) {
    var typeName = types[i];
    constructor[typeName] = i;
    constructor.prototype["is" + typeName] =
    (function (value) {
     return this.type == i &&
     (typeof value == "undefined" || value == this.value);
     });
    }

    constructor.prototype.lookup = function(key) {
      function lookup(key) {
        if (!(this.value.contains(key)))
          return Obj.nullObj;
        return this.value.get(key);
      }
    }

    Object.freeze(constructor.trueObj = new constructor(constructor.Bool, true));
    Object.freeze(constructor.falseObj = new constructor(constructor.Bool, false));
    Object.freeze(constructor.nullObj = new constructor(constructor.Null));
    Object.freeze(constructor.errorObj = new constructor(constructor.Error));
    Object.freeze(constructor.prototype);
    Object.freeze(constructor);

    return constructor;
})();

You can see more of them in the link above.

Could you please advise me some resources which to study to be able to understand the code in the project with ease and even better to contribute later to the project?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:47:35+00:00Added an answer on May 24, 2026 at 10:47 pm

    Working outside in, the first big concept here are anonymous funcitons.

    var Obj = (function() { /* do work */})();
    

    Simply, put, we are making an anonymous function, and then immediately execturing it,
    and assigning the return from the anonymous function to an variable named Obj.

    Why would anyone want to do it?

    In this case, it is used to create a private scope. Local variables in javascript are
    scoped to the function where they are defined. For example:

    function test() {
        var a = 10;
    }
    
    // a is not defined here.
    

    In the last example, a really only exists inside the scope of the function that
    defined it. Javascript is a little tricky this regard because by omitting the var
    keyword, you can define a global variable.

    So in the example you’ve given, they are using this anonymous function to build a
    scope for some variables that will be used, but will ultimately want to be thrown
    away as soon as the function is finished executing.

    Next:

    var Obj = (function() {
        function constructor(type, value) {
            this.type = type;
            this.value = value;
        }
    
        // SNIP
    })();
    

    This creates a new function called constructor. Its important to note that javascript functions
    are first-class objects, which means they work a lot like any other object, and can be assigned
    to a variable. This function is scoped to the anonymous function. So trying to get constructor out
    side of the scope of its function, doesn’t work. For example

    var Obj = (function() {
        function constructor(type, value) {
            this.type = type;
            this.value = value;
        }
    
        // SNIP
    })();
    
    typeof(constructor) // <= undefined
    

    So far, if you were to execute the snippits so far, then Obj would have been undefined. Now
    let’s skip ahead a bit to end, and look at the return.

    var Obj = (function() {
        function constructor(type, value) {
            this.type = type;
            this.value = value;
        }
    
        // SNIP
    
        return constructor;
    })();
    

    So when the anonymous function gets invoked, it returns the constructor. This function that gets
    passed back is assigned to Obj. This hands back the constructor, out of the local scope of the
    function, and assigns to the variable. You would then be able to invoke it

    var Obj = (function() {
        function constructor(type, value) {
            this.type = type;
            this.value = value;
        }
    
        // SNIP
    
        return constructor;
    })();
    
    var o1 = new Obj("t", "v");
    o1.type // <= "t"
    o1.value // <= "v"
    

    Next we’ve got an interesting line

    var Obj = (function() {
        function constructor(type, value) {
        this.type = type;
        this.value = value;
        }
    
        constructor.prototype = { };
    
        // SNIP
    
        return constructor;
    })();       
    

    This sets the prototype for the constructor to an empty object. Explaining the details of prototypes is
    a bit of the scope of this post, but an over-simplication is that a prototype defines the instance methods
    that are available to an object that is created by a constructor. A single ‘prototype’ is shared between
    and will be used to define the methods that will be available to the objects that are constructed by
    calling new Obj().

    Next we have a locally defined array

    var types = [
        "Bool", "Int", "Real", "String", "Name", "Null",
        "Array", "Dict", "Stream", "Ref",
        "Cmd", "Error", "EOF", "None"
    ];
    

    Remember because we are inside of a function, this variable is bound within the outer anonymous function’s scope.

    Next we loop through that array, and set up some stuff.

    for (var i = 0; i < types.length; ++i) {
        var typeName = types[i];
        constructor[typeName] = i;
        constructor.prototype["is" + typeName] =
            (function (value) {
                return this.type == i &&
                (typeof value == "undefined" || value == this.value);
            });
    }
    

    Two things interesting happen here. First, it sets a ‘static’ property off of the constructor, and then
    creates a new function on the constructor’s prototype. This function is called "is" + typeName. So we should
    generate a bunch of instance methods named stuff like: “isBool”, “isInt”, “isReal”, and so on.

    constructor.prototype.lookup = function(key) {
      function lookup(key) {
        if (!(this.value.contains(key)))
          return Obj.nullObj;
        return this.value.get(key);
      }
    }
    

    Next we define another instance method called lookup and does some work.

    Lastly we create some static properties off of our constructor, and freeze them (so they can’t be changed or extended)

    Once its all said and done, Obj, should point to a constructor function, and we should be able to say things like:

    var myType = new Obj("MyType",undefined);
    myType.isBool(undefined) //instance method
    Obj.Bool  // static property
    

    Anyway, I hope that helped a little bit explaining a few of the concepts that were being used. The big take-away should
    be that a function can be used to control scope, and that functions are First-class functions, and can be handed around
    like variables. You can also reference a property off a object using dot notation (obj.property) or bracket notation
    (obj["property"]).

    There are a bunch of more things to learn, and all of the book suggestions are solid in this thread. If it hasn’t been mentioned I
    would also recommend Eloquent JavaSript by Haverbeke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, currently I am refactoring one of my programs, and I found an interesting
I found one source which successfully overrode Time.strftime like this: class Time alias :old_strftime
I found one example in which buttons are added to panels (instances of JPanel
I was playing with a project of mine today and found an interesting little
I found the optional parameters feature in C# 4.0 very interesting, so I tried
Today I found one interesting thing. I didn't know that one can't declare a
I found an interesting article at MSDN, which says: ADO makes it possible to
Sorry for my English, let speak from my heart :) In one project which
Looking at some algorithm exercices on the net, I found an interesting one :
i've found one cheat sheet for doctrine: cheat sheet but it doesn't list all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.