JavaScript报错TypeError: showToastrMessage.init is not a function 求助
Hey there! Let's break down exactly why you're hitting this error and how to fix it.
The Root of the Problem
Looking at your code, you've defined showToastrMessage as a plain function:
var showToastrMessage = function(){ toastr.success('sdfsdfd'); }
But then you're trying to call an .init() method on it like it's an object or class with built-in methods:
showToastrMessage.init() // This fails because plain functions don't have an `init` property
That's why the console throws TypeError: showToastrMessage.init is not a function—the function you created doesn't have an init method attached to it.
Two Simple Fixes
Fix 1: Just Call the Function Directly
If your goal is simply to run the toastr message when the document is ready, you don't need .init() at all. Just invoke the function directly:
var showToastrMessage = function(){ toastr.success('sdfsdfd'); } jQuery(document).ready(function() { showToastrMessage(); // No .init() needed—just call the function normally });
Fix 2: Restructure as an Object with an init Method
If you want to organize your code using an object-based pattern (common for grouping related functionality), redefine showToastrMessage as an object that contains the init method:
var showToastrMessage = { init: function() { toastr.success('sdfsdfd'); } }; jQuery(document).ready(function() { showToastrMessage.init(); // Now this works—we're calling the method on the object });
Quick Extra Check
Make sure the toastr library is properly loaded on your page before your code runs. If toastr isn't loaded, even a correctly called toastr.success() will throw an error!
内容的提问来源于stack exchange,提问作者Adnan Rafique

