Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Object.Function (* is not a function)

How would I add a function after an object, without raising the error 'substr is not a function' ?

e.g.

    var country = {
        get: function() {
            return 'USA';
        },
        set: function(value) {
            
        }
    }

    console.log(country.substr(0, 2));
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You are trying to use substr() in an object that only has .get() and .set(). susbtr() is a method of string, so you can chain it after .get() that actually returns a string:

var country = {
        get: function() {
            return 'USA';
        },
        set: function(value) {
            
        }
    }

    console.log(country.get().substr(0, 2));

about 4 years ago · Juan Pablo Isaza Report

0

I am not sure what you mean add a function after an object. But if you want to log the substring of "USA", you are missing the call of the function in your log. Country is an object. It does not have any function substring. You have to define it first. You should call get() function to retrieve the value. It should be:

country.get().substr(0, 2)
about 4 years ago · Juan Pablo Isaza Report

0

Country is an object, you can't use substring on an object.

You could do

country.get().subsring(0,2)

On another note, this is very un-javascript like code. Looks like you're trying todo OO programming in javascript.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!