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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:52:23+00:00 2026-06-12T00:52:23+00:00

I am using Knockout.js for a rich client application and it will consist of

  • 0

I am using Knockout.js for a rich client application and it will consist of large number of knockout.js ViewModels. In the development, I noticed two ways of creating knockout.js ViewModels.
First way.

function AppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");}

Second way.

var appViewModel = {
this.firstName = ko.observable("Bert"),
this.lastName = ko.observable("Bertington")};

Is there any specific difference in these two methods of declaring ViewModels?
In knockout.js official page examples they have used the first way. But in third party frameworks like Knockout-validations.js has used second way. Which way should I use? Any specific advantage in using it?

I found out if I use first way, then I cant use Knockout-validations.js framework. I am really confused on this matter. Any comment is appreciated.

Thank you.

  • 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-06-12T00:52:24+00:00Added an answer on June 12, 2026 at 12:52 am

    The first way defines an object constructor but does not instantiate a new object, you can pass in arguments this way. If you’re creating multiple objects/models this would definitely be less clunky than the second way.

    The second way is using object initializer syntax, this instantiates a new object in memory with whatever fields you set it to. In general this produces for smaller code size; if you’re creating two objects of the same or similar structures, use the first way.

    There is no reason why you can’t use the first in place of the second. Simply creating a new object and pass it in wherever it is needed.

    This:

    function AppViewModel(fName, lName) {
        var self = this;
        self.firstName = ko.observable(fName);
        self.lastName = ko.observable(lName);
    }
    ...
    var appViewModel = new AppViewModel("Bert", "Bertington");
    

    creates the same object as this:

    var appViewModel = {
        this.firstName = ko.observable("Bert"),
        this.lastName = ko.observable("Bertington")
    };
    

    The first one just allows for more AppViewModels to be created, e.g. new AppViewModel("Joe", "Shmoe")

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

Sidebar

Related Questions

I'm working with a new application at work that we're building out using Knockout.js
i am using knockout on web application which displays the data with a foreach
I am creating a guest list app using Knockout.js, and so far things are
I am using knockout and have an array Properties and each Property can have
I am using knockout.js in my ASP.NET MVC project. I figured out how to
I just started using knockout.js and it works great with normal bidings. I have
A bit of a mystery has cropped up using knockout (2.1.0) and knockout mapping
I am trying to create a group of dropdowns using knockout out that allow
I want to say hello to the stackoverflow community. I've just started using knockout
I'm using both Knockout (version 2.0) and jQuery Mobile (version 1.0.1) in the same

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.