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

  • Home
  • SEARCH
  • 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 7658615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:13:30+00:00 2026-05-31T13:13:30+00:00

function Cons(name) { var pvar1=hi +name; sayhi = function() { alert(pvar1); } attEvents(){ $(‘#’+name).bind(click,sayhi);

  • 0
function Cons(name) {
var pvar1="hi "+name;
sayhi = function() {
   alert(pvar1);
}
attEvents(){
$('#'+name).bind("click",sayhi);
}
}
var a = new Cons('name1');
var b = new Cons('name2');
var c = new Cons('name3');

Let us assume that name1, name2, name3 are all divs. Now, whatever div I click, I am always getting “hi name3”. The private variable is having the last stored value in it irrespective of the div clicked. Any help is appreciated.

Thanks

  • 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-31T13:13:30+00:00Added an answer on May 31, 2026 at 1:13 pm
    sayhi = function() {
       alert(pvar1);
    }
    

    If you define it without var keyword then it is defined as a global. Thus sayhi refers to the function defined as a last one. Use

    var sayhi = function() {
       alert(pvar1);
    }
    

    or

    var pvar1 = "hi "+name,
        sayhi = function() {
            alert(pvar1);
        };
    

    Note the comma. Or true jQuery style

    $('#'+name).bind("click", function() {
        alert(pvar1);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function check(id){ var _id = document.getElementById(id); url = test.php?check=1&id= + _id; } i want
I've recently started learning scala, and I've come across the :: (cons) function, which
Was thinking of creating a CONS function to create a list, but then realized
class X { static int i; // some other function, variable declaration, cons, dest
I searched Stack Overflow for the pros/cons of function-like macros v. inline functions. I
What are the pros and cons of function-level scope specifically in Javascript compared to
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
Function FillAdminAccount() As Boolean FillAdminAccount = True Try SQLconn.ConnectionString = connect timeout=9999999; & _
function Submit_click() { if (!bValidateFields()) return; } function bValidateFields() { /// <summary>Validation rules</summary> ///
function main() { Hello(); } function Hello() { // How do you find 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.