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 6743917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:01:18+00:00 2026-05-26T12:01:18+00:00

I was searching over the Internet for good JS practices and found an interesting

  • 0

I was searching over the Internet for good JS practices and found an interesting one (that sadly, I don’t understand it entirely)

myNameSpace = function(){
  var current = null;
  function init(){...}
  function change(){...}
  function verify(){...}
  return{
    init:init,
    change:change
  }
}();
  • Where can I use this name space?
  • How can I use the names pace?
  • Is it really good idea to use this instead of Global?

Source of script: http://dev.opera.com/articles/view/javascript-best-practices/

  • 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-26T12:01:18+00:00Added an answer on May 26, 2026 at 12:01 pm

    You can also use a code like this, that lets you write OO JavaScript code. You should group only cohesive functions in one unit and build more objects like this as needed.

    Note that function buildFullName is a private function, since it can’t be accessed from outside.

    I agree with you that is confusing, without previous JavaScript knowledge. The fact is that a function is a first class Object in JavaScript. You can nest functions within functions, they can have variables, and there are so many ways to combine them, there is no standard way like in Java. I think the article JavaScript Closure 101 can help you clear things up a bit.

    /** This is our Object definition and constructor */
    function Person(fname, lname) {
        this.firstName = fname;
        this.lastName = lname;
    }
    
    /** Anonymous function to avoid polluting the global namespace */
    (function () {
    
        /** This function will be defined public, we prefix the class name */
        function Person_toString() {
            //call buildFullName passing this, we could also call the function
            //as buildFullName(),  and it would work, but wouldn't have 'this'
            return buildFullName.call(this);
        }
    
        /** Another public function */
        function Person_buildGreeting() {
            return "Hi " + this.firstName;
        }
    
        /** This function is private! */
        function buildFullName() {
            return this.firstName + " " + this.lastName;
        }
    
        /** Here we augment the Object with public functions */
        Person.prototype = {
            toString: Person_toString,
            buildGreeting: Person_buildGreeting
        };
    
    })(); //execute the anonymous function immediately
    

    Example usage:

    var p1 = new Person('Jenny', 'Fairview');
    alert("toString()=" + p1.toString());
    alert("buildGreeting()=" + p1.buildGreeting());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching all over the Internet, but did not find that exact
I was searching about process model of Erlang over internet and found out some
I have been searching for this all over the internet, this site claims that
I've been searching all over the place and just don't see anyone doing it
When is one preferred over the other when searching for nested Divs?
Searching here I found that this question was already asked , but I think
I've been searching all over the internet and zig-zagging through the apple site, but
Hey people I've been searching over the internet for the following problem without success...
I have been searching all over the internet for an answer to this. It
I have been searching all over the internet tonight, saw a lot of solutions

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.