调用JavaScript构造函数方法报错:cohort1.sayName不是函数
Hey there! Let's dig into why you're seeing that cohort1.sayName is not a function error—this is a super common gotcha with JavaScript constructor functions, so let's break down the most likely culprits and fixes.
1. You didn't attach the sayName method to the constructor's prototype
If you define the method inside the constructor without assigning it to this, or forget to link it to the prototype entirely, your instances won't have access to it. Here's what that mistake looks like:
// ❌ Wrong: Method isn't connected to the Cohort prototype function Cohort(name) { this.name = name; } // This function exists on its own, not linked to Cohort instances function sayName() { console.log(this.name); } const cohort1 = new Cohort("Frontend Bootcamp"); cohort1.sayName(); // Error: Not a function
Fix it by attaching the method to the constructor's prototype:
// ✅ Correct: Method is part of the Cohort prototype function Cohort(name) { this.name = name; } // Link sayName to all Cohort instances via the prototype Cohort.prototype.sayName = function() { console.log(`Cohort name: ${this.name}`); }; const cohort1 = new Cohort("Frontend Bootcamp"); cohort1.sayName(); // Outputs "Cohort name: Frontend Bootcamp"
Alternatively, you can assign the method directly to this inside the constructor (though using the prototype is more memory-efficient for multiple instances):
function Cohort(name) { this.name = name; this.sayName = function() { console.log(this.name); }; }
2. You forgot the new keyword when creating an instance
If you call the constructor without new, JavaScript won't create a new instance of Cohort. Instead, this will point to the global object (like window in browsers), and the function will return undefined by default. That means cohort1 is undefined, so calling sayName() throws an error.
// ❌ Wrong: No new keyword const cohort1 = Cohort("Frontend Bootcamp"); cohort1.sayName(); // Error: Cannot read property 'sayName' of undefined
Fix it by using new to instantiate your object:
// ✅ Correct: Using new creates a proper Cohort instance const cohort1 = new Cohort("Frontend Bootcamp"); cohort1.sayName(); // Works as expected
3. Your constructor returns an unrelated object
If your constructor function returns a custom object (instead of relying on the default this instance), the new keyword will return that object instead of your Cohort instance. Since that custom object doesn't have the sayName method, you'll get the error.
// ❌ Wrong: Constructor returns a different object function Cohort(name) { this.name = name; // This overrides the default instance returned by new return { randomProp: "oops" }; } Cohort.prototype.sayName = function() { ... }; const cohort1 = new Cohort("Test"); cohort1.sayName(); // Error: The returned object has no sayName method
Fix it by removing the custom return statement (or return this explicitly if you need to):
function Cohort(name) { this.name = name; // No return statement, so JS returns the this instance automatically }
4. Typos in the method name
Double-check that you spelled sayName exactly the same way when defining it and calling it. A lowercase n (sayname) or extra character will break everything!
If none of these fix the issue, feel free to share your full constructor code—we can narrow it down further.
内容的提问来源于stack exchange,提问作者Devin Bowen

