{"id":32522,"date":"2023-01-30T11:28:08","date_gmt":"2023-01-30T05:58:08","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/"},"modified":"2023-01-30T11:28:08","modified_gmt":"2023-01-30T05:58:08","slug":"solved-how-to-get-specific-value-of-the-object","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/","title":{"rendered":"[Solved] How to get specific value of the object?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-25994366\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"25994366\" data-parentid=\"25994258\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><code>new myObj<\/code> creates and returns a new object, even though you have <code>return 7<\/code> in your function. The <code>new<\/code> operator is specifically for creating objects, and has the interesting behavior of completely ignoring the return value of the constructor function <em>unless<\/em> it&#8217;s a non-<code>null<\/code> object reference; instead, <code>new<\/code> returns the object it created. (If you <em>do<\/em> return a non-<code>null<\/code> object reference from the constructor function, it overrides <code>new<\/code>&#8216;s default behavior; use cases for this are rare.)<\/p>\n<p>If you just want to call the function and get its return value, don&#8217;t use <code>new<\/code>:<\/p>\n<pre><code>console.log(myObj());\n<\/code><\/pre>\n<p>If you want to create an object with a property on it, and set that property to <code>7<\/code>, then print it, you need to create the property:<\/p>\n<pre><code>function myObj() {\n    this.thePropertyName = 7;\n}\n<\/code><\/pre>\n<p>&#8230;and then use it:<\/p>\n<pre><code>var o = new myObj();\nconsole.log(o.thePropertyName);\n<\/code><\/pre>\n<p>&#8230;or:<\/p>\n<pre><code>console.log(new myObj().thePropertyName);\n<\/code><\/pre>\n<p>&#8230;but then you&#8217;re creating and releasing an object for no good reason.<\/p>\n<hr>\n<p>Re your edit, which changes the question <strong><em>markedly<\/em><\/strong>:<\/p>\n<blockquote>\n<p>this task:<\/p>\n<pre><code>new myObj() \/\/ return 1; new myObj() \/\/ return 2;\n\nnew myObj() + new myObj() = \/\/ return 7;\n<\/code><\/pre>\n<\/blockquote>\n<p>That&#8217;s a bizarre requirement. It breaks down like this:<\/p>\n<ol>\n<li>\n<p>Every time you call <code>new myObj<\/code>, save a number (starting with <code>1<\/code>).<\/p>\n<\/li>\n<li>\n<p>When the <code>+<\/code> operator is used on the objects, return that number.<\/p>\n<\/li>\n<\/ol>\n<p>You <strong>can<\/strong> do it, by using a common counter, a value on the instance, and overriding the default implementations of <code>toString<\/code> and <code>valueOf<\/code>; see comments in the code:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>(function() {\r\n  \"use strict\";\r\n\r\n  \/\/ Use a scoping function to build `myObj` so that we\r\n  \/\/ keep the counter private\r\n  var myObj = (function() {\r\n    \/\/ The counter\r\n    var counter = 0;\r\n\r\n    \/\/ The `myObj` function we'll return\r\n    function myObj() {\r\n      \/\/ Increment the counter, remember the number\r\n      this.value = ++counter;\r\n    }\r\n    \r\n    \/\/ When a coercion to number is preferred, JavaScript will\r\n    \/\/ use the valueOf function\r\n    myObj.prototype.valueOf = function() {\r\n      return this.value;\r\n    };\r\n    \r\n    \/\/ When a coercion to string is preferred, JavaScript will\r\n    \/\/ use the toString function\r\n    myObj.prototype.toString = function() {\r\n      return String(this.value);\r\n    };\r\n\r\n    \/\/ Return `myObj` from our scoping function to export it\r\n    return myObj;\r\n  })();\r\n\r\n  \/\/ Using it\r\n  display(new myObj());               \/\/ \"1\"\r\n  display(new myObj());               \/\/ \"2\"\r\n  display(new myObj() + new myObj()); \/\/ 7\r\n\r\n  \/\/ Utility function\r\n  function display(msg) {\r\n    var p = document.createElement(\"p\");\r\n    p.innerHTML = String(msg);\r\n    document.body.appendChild(p);\r\n  }\r\n})();<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to get specific value of the object? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] new myObj creates and returns a new object, even though you have return 7 in your function. The new operator is specifically for creating objects, and has the interesting behavior of completely ignoring the return value of the constructor function unless it&#8217;s a non-null object reference; instead, new returns the object it created. (If &#8230; <a title=\"[Solved] How to get specific value of the object?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/\" aria-label=\"More on [Solved] How to get specific value of the object?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333],"class_list":["post-32522","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How to get specific value of the object? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to get specific value of the object? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] new myObj creates and returns a new object, even though you have return 7 in your function. The new operator is specifically for creating objects, and has the interesting behavior of completely ignoring the return value of the constructor function unless it&#8217;s a non-null object reference; instead, new returns the object it created. (If ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-30T05:58:08+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to get specific value of the object?\",\"datePublished\":\"2023-01-30T05:58:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/\"},\"wordCount\":217,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/\",\"name\":\"[Solved] How to get specific value of the object? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2023-01-30T05:58:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-get-specific-value-of-the-object\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to get specific value of the object?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to get specific value of the object? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to get specific value of the object? - JassWeb","og_description":"[ad_1] new myObj creates and returns a new object, even though you have return 7 in your function. The new operator is specifically for creating objects, and has the interesting behavior of completely ignoring the return value of the constructor function unless it&#8217;s a non-null object reference; instead, new returns the object it created. (If ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/","og_site_name":"JassWeb","article_published_time":"2023-01-30T05:58:08+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to get specific value of the object?","datePublished":"2023-01-30T05:58:08+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/"},"wordCount":217,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/","name":"[Solved] How to get specific value of the object? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-30T05:58:08+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-get-specific-value-of-the-object\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to get specific value of the object?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=32522"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32522\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=32522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=32522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=32522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}