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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:51:30+00:00 2026-05-20T16:51:30+00:00

I’m sorry if this question has been asked before, but I’m not even sure

  • 0

I’m sorry if this question has been asked before, but I’m not even sure what search terms to use to find the answer and when I try to search I never get anything specific to this question.

I’m using Javascript and I am wondering if it is possible to do something like this:

find(x);             // find a document (for example)
find.inFolder(y);    // find a folder's documents (for example)

In other words, can I have a function that can also be used as an object/class? I know I could run find() once and return a hash so that find.inFolder() would work, but I’m hoping there’s a way where I could continue to call find().

Can it be done with prototype? (my “prototype” knowledge is very limited)

function find() {}
find.prototype.inFolder = function() {}

Can it be done inside a hash? [I know this code doesn’t work]

var find = {
    () : function() {},
    inFolder : function() {}
}

To push it even further, is there a way to have the results of .inFolder() be sent to the find() function this way:

find().inFolder();

I know you might say that I don’t understand the concept of javascript, and you’d be mostly correct, but I’ve seen people do some pretty amazing stuff with JS so I thought I’d ask the pros out there.

Thanks in advance for any help.

  • 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-20T16:51:31+00:00Added an answer on May 20, 2026 at 4:51 pm

    What you’re describing is a Fluent interface (if you want something to search for). You could accomplish something like what you’re trying to achieve like this:

    var find = function() {
        this.inFolder = function() {
            return this; // Although to stop chaining, you could return nothing here.
        };
        return this;
    };
    
    find().inFolder(); // .inFolder().inFolder()...
    

    This is a great pattern, especially when leveraged in projects like jQuery:

    $("#element").find(".child_element").first();
    

    Each call returns a jQuery object with .find(), .first() and many other functions, which lets you write intuitive and fluid code.


    I kind of liked your find().inFolder() example, so here’s an expanded version:

    var find = function(file) {
        this.folders = {
            "Documents": ["Foo.txt", "Bar.txt"],
            "Downloads": ["File.exe"],
            "Misc": ["Picture.jpg"]
        };
    
        this.file = file;
    
        this.inFolder = function(folder) {
            var files = this.folders[folder];
            return files.indexOf(this.file) >= 0;
        };
        return this;
    };
    
    alert(find("Foo.txt").inFolder("Documents")); // True
    alert(find("File.exe").inFolder("Downloads")); // True
    alert(find("Picture.jpg").inFolder("Downloads")); // False
    

    http://jsfiddle.net/andrewwhitaker/TCdTd/

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I need to clean up various Word 'smart' characters in user input, including but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I used javascript for loading a picture on my website depending on which small
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.