我以一种奇怪的方式使用eval函数,作为构造函数。
try {
var y = new eval()
} catch(error) {
console.log("caught a " + error.name + ": " + error.message);
}
它会抛出错误,
caught a TypeError: function eval() { [native code] } is not a constructor
正如错误消息所显示的,eval是一个函数,而不是一个构造函数。
问题是,不是所有的javascript函数都充当构造函数吗?
我试图在另一个方法中使用一个方法作为构造函数。但是,当我这样做时,我会得到以下TypeError:
TypeError: function is not a constructor (evaluating 'new self.f(1)')
示例代码是:
class C{
constructor(v){
this.f(v);
this.g(v);
}
f(v){
this.v = v;
}
g(v){
我已经接管了一个研究项目,并且一直做得很好,除了最近,我想我误解了一些JS结构,它是在主干中实现的。对于模型或集合的结构,以及它是返回对象还是函数,我感到困惑。
该项目目前的模型结构如下:
define([…], function(…) {
var measureModel = Backbone.Model.extend({
defaults: {…},
initialize: function(){…}
…
});
return measureModel; //here there is no function, so I assume its an ob
ECMAScript规范函数返回真当且仅当它的参数有一个[Call]内部方法。它在规范中的几个地方使用,例如在的定义中。
有一个类似的规范函数,它返回真当且仅当它的参数有一个[构造]内部方法。
一些JS对象,包括大多数内置函数(如escape )都是可调用的,但不可构造。有可构造但不可调用的吗?
请注意,当调用为普通函数时,用户定义类和内建类都会抛出TypeError,但根据IsCallable的定义仍然是可调用的,这可以通过查看Array.prototype.toString是否尝试将它们用作join的实现来确定。
// {} is not callable, so toString f
我决定为Express服务器编写ES6类,但是extended覆盖子原型,没有办法使用定义外部构造函数的方法和道具。见代码:
import Express from 'express'
export default class Server extends Express {
constructor(options) {
super(options.expressOptions || {})
super.use(cookieParser()) // TypeError: (intermediate value).use is not a
使用此代码:
function thing(){
function majig(){
alert("done");
}
}
var mything = new thing();
mything.majig();
我得到了一个错误: TypeError: mything.majig不是一个函数
我做javascript已经有一段时间了,我已经将函数作为函数的一部分来完成,并且以前调用过它们。我知道这必须是一些简单的东西,我错过了或者忘记了,但是各种各样的网络搜索(以及在这里四处窥探)让我得到了更深层次的理论答案,或者似乎表明这是可行的例子。
我知道
我正在上阅读有关类的文章,并看到一个使用get关键字定义方法的示例。在这里,我注意到不需要使用圆括号(分组操作符())来通过类的实例调用这样的方法(使用get关键字定义)。
比如,在下面的例子中,
square.area语法调用Rectangle类的area方法。
但是,Uncaught TypeError: square.area is not a function.会引发square.area()错误。
有人能解释一下我在这里错过了什么吗?
这里是一个例子:
class Rectangle {
constructor(height, width) {
thi
我在运行以下代码时遇到错误:
class Person:
def _init_(self, name):
self.name = name
def hello(self):
print 'Initialising the object with its name ', self.name
p = Person('Constructor')
p.hello()
输出为:
Traceback (most recent call last):
File "./class_init.py", line 11, in &
下面的代码抛出一个TypeError
import tensorflow as tf
h=tf.int32(6)
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'DType' object is not callable
为什么?
据我所知,箭头函数类似于普通函数。当我这样使用时,没有问题:
let X = () => {};
let Y = function() {};
X();
Y();
但是,当我将它们与new一起使用时出现错误
let X = () => {};
let Y = function() {};
x = new X();
y = new Y();
Uncaught TypeError: X is not a constructor
你能给我解释一下为什么吗?非常感谢。
使用JRuby 1.6.0RC1
我有一个java文件
package com.foo.bar
public class Foo
{
Foo(String baz){}
}
如果,在jruby里,我知道
com.foo.bar.Foo.new "foo"
然后我得到
TypeError: no public constructors for Java::ComFooBar::Foo
读让我觉得这是WAD,但是如何在不修改java文件的情况下解决这个问题呢?
子类Foo,然后实例化我得到一个不同的错误:
ArgumentError:构造函数调用失败:尝试从类org.jruby
我们可以使用Object.prototype.toString.call(foo)来检测对象类(foo的类型),它工作得很好。
但是为什么Object.toString.call({})要抛出TypeError: Function.prototype.toString is not generic
Object.toString不是从Object.prototype继承的吗
为什么
const Todos = function () {
...
}
const todos = new Todos();
工作很好,但是
const Todos = () => {
...
}
const todos = new Todos();
给一个TypeError: Todos不是构造函数错误吗?
我用java写了一个类,我想用jython在python中执行它。首先是我得到的错误吗?
Traceback (most recent call last):
File "/Users/wolverine/Desktop/project/jython_scripts/customer.py", line 3, in <module>
customer = Customer(1234,"wolf")
TypeError: No visible constructors for class (Customer)
我的Java类格式:
publ
>> typeof Object
"function"
>> var Object = new Object();
>> typeof Object
"object"
>> var a = new Object()
TypeError: Object is not a constructor
为什么可以使用"Object“作为有效的变量名?
好吧,看起来我没有提供足够的信息。因此,最终目标是运行基于某些子网的goal扫描。因此,我创建了一个包含子网的文本文件,然后运行runs扫描,并提供所有IP可达的输出。 Here is the pingsweep code 我得到了这个错误: Traceback (most recent call last):
File "pingsweep1.py", line 118, in <module>
sub = subnet(line)
TypeError: this constructor takes no arguments 任何帮助都将不胜感激。
可能重复:
BigDecimal不像初始值那样接受浮点,所以我正在编写一个构造函数来处理它。它似乎忽略了初始化方法,并调用了默认构造函数。
它抛出TypeError can't convert Float into String (TypeError)
format方法确实有效。
文件BigDecimal.rb:
require 'bigdecimal'
class BigDecimal
def initialize
if self.class == Float
super self.to_s
e
我注意到并非所有Javascript函数都是构造函数。
var obj = Function.prototype;
console.log(typeof obj === 'function'); //true
obj(); //OK
new obj(); //TypeError: obj is not a constructor
问题1:如何检查函数是否是构造函数,以便可以使用new关键字调用它?
问题2:当我创建一个函数时,是否可以让它成为而不是构造函数?
我试图构建一行代码来动态地更改函数名,通过这样做,我将每个函数的名称存储在一个变量中(从html元素获取,这并不重要)。不管怎样,我想做的是:
var validateType = $this.data("type");
new Validator.validateType($this.val(), validateLength)
验证器是一个对象,当然"validateType“是该对象中每个子函数的名称。"validateType“也是变量。
我得到这个错误是因为一个错误:TypeError: validator.validateType is not a
Person = (name, age, job) ->
@name = name
@age = age
@job = job
@sayName = ->
console.log(@name)
kim = new Person("Kimothy Lozado", 37, "Skip Tracer")
console.log kim.sayName()
返回误差
TypeError: Object function () {
return console.log(this.name);
} has no
我的程序说有一个错误,它不需要任何参数。有谁能发现这个错误吗?在30号线和12号线。谢谢 class City():
#define the structure and content of the Student Record
def _init_(self, name, country, population, language):
self.name = name #name will be a string
self.country = country #country will be a string
self.population = popul
我需要在构造函数中返回一个Bike实例。例如:
class Bike(object):
def __init__(self,color):
self.bikeColor = color
return self #It should return an instance of the class; is this not right?
myBike = Bike("blue")
当我这样做时,我会得到以下错误:
TypeError: __init__() should return None, not 'Bike'
如果是