[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Errors/Not_a_function [Back]  [Original]

TypeError: "x" is not a function - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

TypeError: "x" is not a function

JavaScript is not a function

In this article

TypeError: "x" is not a function. V8 & Firefox & Safari

TypeError

JavaScript Object map JavaScript Array

js
const x = document.getElementByID("foo");
// TypeError: document.getElementByID is not a function

getElementById

js
const x = document.getElementById("foo");

Array.prototype.map() Array

js
const obj = { a: 13, b: 37, c: 42 };

obj.map((num) => num * 2);

// TypeError: obj.map is not a function

js
const numbers = [1, 4, 9];

numbers.map((num) => num * 2); // [2, 8, 18]

js
function Dog() {
  this.age = 11;
  this.color = "black";
  this.name = "Ralph";
  return this;
}

Dog.prototype.name = function (name) {
  this.name = name;
  return this;
};

const myNewDog = new Dog();
myNewDog.name("Cassidy"); // TypeError: myNewDog.name is not a function

js
function Dog() {
  this.age = 11;
  this.color = "black";
  this.dogName = "Ralph"; // Using this.dogName instead of .name
  return this;
}

Dog.prototype.name = function (name) {
  this.dogName = name;
  return this;
};

const myNewDog = new Dog();
myNewDog.name("Cassidy"); // Dog { age: 11, color: 'black', dogName: 'Cassidy' }

2 (3 + 5) 2*(3 + 5) 2(3 + 5)

js
const sixteen = 2(3 + 5);
console.log(`2 x (3 + 5) is ${sixteen}`);
// Uncaught TypeError: 2 is not a function

*

js
const sixteen = 2 * (3 + 5);
console.log(`2 x (3 + 5)  ${sixteen}`);
// 2 x (3 + 5)  16

helpers helpers.js

js
function helpers() {}

helpers.groupBy = function (objectArray, property) {
  return objectArray.reduce((acc, obj) => {
    const key = obj[property];
    acc[key] ??= [];
    acc[key].push(obj);
    return acc;
  }, {});
};

export default helpers;

App.js

js
import helpers from "./helpers";


Web Proxy Viewer  |  New URL  |  Original Page