在javascript中使用具有函数的对象

在javascript中使用具有函数的对象,javascript,Javascript,大家好,我是javascript开发新手。我已经学习了javascript对象,并用它编写了一些代码。我的代码 function afunction() { var num = 10; return num; } afunction.randomfunction(function() { return { "name": "somename" "age": 12 } }) 当我用affunction.randomfunction.name调用函数时它给我的错误如下 " underf

大家好,我是javascript开发新手。我已经学习了javascript对象,并用它编写了一些代码。我的代码

function afunction() { 
var num = 10;
return num;
}

afunction.randomfunction(function() {

return {

"name": "somename"

"age": 12
}

})

当我用
affunction.randomfunction.name调用函数时它给我的错误如下

" underfined is not a function"

我不知道为什么会这样。我需要的是,我需要使用
affunction.randomfunction.name


我知道我做错了什么。希望你们能帮我。找出我的错误。

你正在尝试调用
随机函数
并将其传递给函数表达式,而不仅仅是赋值函数表达式

obj.foo(x)
更改为
obj.foo=x

然后可以调用它-
obj.foo()
,并从中访问返回值:
obj.foo().property



发生这种情况是因为函数
randomFunction
未在
aFunction
中定义。您需要将其定义为这样一个单独的函数

function randomFunction(){
    //code
}
function YourObject(){

    var num = 10; //think of this as a constructor

    this.getNum = getNum; //this "attaches" the getNum() function code to the getNum variable of YourObject
    function getNum(){
        return this.num;
    }

    this.randomFunction = randomfunction;
    function randomFunction(){
        //code
    }
}
var yourObject = new YourObject(); //instantiate
console.log(youObject.getNum()); //print the value of yourObject.num to console
yourObject.randomFunction(); //execute your random function
并通过
randomFunction()调用它

或者,如果你想创建一个带有公共函数的对象,你可以这样创建它

function randomFunction(){
    //code
}
function YourObject(){

    var num = 10; //think of this as a constructor

    this.getNum = getNum; //this "attaches" the getNum() function code to the getNum variable of YourObject
    function getNum(){
        return this.num;
    }

    this.randomFunction = randomfunction;
    function randomFunction(){
        //code
    }
}
var yourObject = new YourObject(); //instantiate
console.log(youObject.getNum()); //print the value of yourObject.num to console
yourObject.randomFunction(); //execute your random function
之后,您可以像这样调用对象的方法

function randomFunction(){
    //code
}
function YourObject(){

    var num = 10; //think of this as a constructor

    this.getNum = getNum; //this "attaches" the getNum() function code to the getNum variable of YourObject
    function getNum(){
        return this.num;
    }

    this.randomFunction = randomfunction;
    function randomFunction(){
        //code
    }
}
var yourObject = new YourObject(); //instantiate
console.log(youObject.getNum()); //print the value of yourObject.num to console
yourObject.randomFunction(); //execute your random function

注意:在JavaScript中创建对象有很多方法,这只是其中之一。您可能会发现很有趣。

affunction.randomfunction.name
在此上下文中不是正确的代码,因为
randomfunction
不存在,也不是
affunction
的子项。你为什么有这个代码?你想要实现什么?
affunction
没有
randomfunction
的属性,所以
affunction.randomfunction
未定义的
@Dai我只想通过使用affunction.randomfunction(function(){})来获取名称的值@xdazz您能给我举个例子来回答向randomfunction添加属性的问题吗?这样我就可以更好地理解
afunction.randomfunction
应该是什么?它应该是一个函数吗?如果是这样的话,你必须先将它定义为一个函数,然后才能调用它。你能给我一个例子吗?我想调用两个函数,比如function1.function2(function(){object_here}})…然后从这里返回对象值。呃…我给你提供了一个例子!这就是我想要的答案。当然我会接受这个答案。我怀疑如果我像function.randomfunction(function(){return{name:somename};})一样使用它是否有效;这不起作用..它说的是SyntaxError:Unexpected token)我不想这样做,我想按照@quentin说的做,但当我运行他的代码时,它会显示SyntaxError:Unexpected token)