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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:15:47+00:00 2026-05-23T07:15:47+00:00

I have this Car function: var Car = function(vendor, model, year) { return {

  • 0

I have this Car function:

var Car = function(vendor, model, year) {
    return {
        vendor: vendor,
        model: model,
        year: year,
        name: (function() {
            return vendor + " " + model + " " + year;
        })()
    };
};
var foo = Car("Toyota","Corola",2007);
alert(foo.name);  //alerts "Toyota Corola 2007"

This works, but I want the name to be able to change according to the vendor, model, and year.

taxi.vendor = "Mitsubishi";
alert(taxi.vendor); //still alerts "Toyota Corola 2007"

How can I instead make it alert Mitsubishi Corola 2007 according to the change of the vendor property?

EDIT: And the catch — name must remain a property that does not need to be called as a function.

  • 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-23T07:15:48+00:00Added an answer on May 23, 2026 at 7:15 am

    With recent versions of WebKit (Safari, Chrome) or Firefox, you can define getter and setter functions:

    var o = {a: 7, get b() {return this.a + 1;}, set c(x) {this.a = x / 2}};
    o.b // result is 8
    o.a = 10
    o.b // result is 11
    

    Then you would do this:

    var Car = function(vendor, model, year) {
        return {
            vendor: vendor,
            model: model,
            year: year,
            get name() { return this.vendor + " " + this.model + " " + this.year; }
        };
    };
    

    And get the result that you want.

    I don’t know if IE or Opera support this or which versions. If you need to support anything other than recent Safari, Chrome, or Firefox browsers then you’re better off using a function to access the name instead of leaving it as a property:

    var Car = function(vendor, model, year) {
        return {
            vendor: vendor,
            model: model,
            year: year,
            name: function() { return this.vendor + " " + this.model + " " + this.year; }
        };
    };
    

    And then:

    var foo = Car("Toyota","Corola",2007);
    alert(foo.name());  //alerts "Toyota Corola 2007"
    foo.vendor = "Mitsubishi";
    alert(foo.name());  //alerts "Mitsubishi Corola 2007"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array: data.Dealer.car[0] data.Dealer.car[1] data.Dealer.car[2] If I do this: alert(data.Dealer.car.length); delete data.Dealer.car[1];
say I have a function: public List<Car> GetFourWheelDrives() { return dataContext.Cars.Where(c => c.IsFourWheelDrive ==
for examples i have this text: cat car dog... . Impossible that trim function
I have this array var car = new Array(); car['brand'] = Ford; car['color'] =
I have a function in testFun.js var testFunc=function{ var hasCar=false; return{ checkCar: function(){ for(var
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have this line in a javascript block in a page: res = foo('<%=
I have a query like: var function = GetSomeExpression(); using (FooModel context = new
I have this data structure (basically): (setq ssm-list '(tasklist ((id . 10525295) (name .
I have found this example on StackOverflow: var people = new List<Person> { new

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.