将对象作为函数调用以及调用该对象内的函数

时间:2016-01-29 21:19:53

标签: javascript

我希望有人向我确认,创建一个允许我在javascript中进行以下两个调用的对象是不可能的:

user.remove();
user.remove.all();

2 个答案:

答案 0 :(得分:4)

当然可以。这是一个例子。

void checkMessage(int sock){
int n;
char message[256];
n = read(sock, message, 255);
int response;

if(n < 0){
    perror("Error reading the socket");
    exit(1);
}

//see if the message is from the robot or from the interface
if(strstr(message,"robot") != NULL) {
    //The robot wants the co-ordinates of the component
    robotComms(sock);
} else {
    //Someone has accessed the main page
    interfaceComms(sock);
}
}

void interfaceComms(int sock) {
int n;

n = write(sock, interfaceConnection->processingResults, 47);

if (n < 0){
    perror("Error writing to the socket");
    exit(1);
} else {
    printf("Results sent to interface\n");
}
}

void robotComms(int sock) {
int n;
srand ( time(NULL) ); //reset rand
//printf("Here is the message: %s\n", message);
printf("Getting image...\n");

//Change currImage to prevImage
imageDetails->prevImage = imageDetails->currImage;

答案 1 :(得分:0)

不,这不是不可能的。函数就像其他所有东西一样。没有什么可以阻止你做以下事情:

slice(...).merge(...)

然后正常创建用户:

function User() {
    var remover = function (){
        // do removal stuff 
    }

    remover.all = function () {
        // do remove all stuff
    } 

    this.remover = remover;
}