在这种情况下,在不使用javascript对象的情况下解决此问题

在这种情况下,在不使用javascript对象的情况下解决此问题,javascript,Javascript,我曾经用python编程,但现在我在一些项目中使用javascript。在python中,OOP的概念比我现在正在阅读的javascript要简单得多。话虽如此,我现在对使用javascript对象和原型不是很舒服。同时,我能在不使用object的情况下实现以下功能吗?我不是一个纯粹的程序员(总是把它当作业余爱好),所以请容忍我: function func1(arg1){ does item..1..for eg...var result=arg1*2; does item..2..for

我曾经用python编程,但现在我在一些项目中使用javascript。在python中,OOP的概念比我现在正在阅读的javascript要简单得多。话虽如此,我现在对使用javascript对象和原型不是很舒服。同时,我能在不使用object的情况下实现以下功能吗?我不是一个纯粹的程序员(总是把它当作业余爱好),所以请容忍我:

function func1(arg1){

does item..1..for eg...var result=arg1*2;
does item..2..for eg...var resultSq=arg*arg;
......
}
我想要其他函数,比如func2,它在调用func1时;funct1仅通过执行项…1并返回结果来响应

换句话说,我想检测是谁调用了函数,并使用if语句来更改执行 如果太简单,很抱歉。

识别来电者:
有许多模式可以创建和处理对象,下面是一个我经常使用的模式

JavaScript原型OOP

var ConstructFoo = function () { //capitalise constructor names
    var x = "my private variable";//remember, JS has FUNCTIONAL scope
    this.y = 10; //we can read and write to this outside of this function
}
ConstructFoo.prototype.barMethod = function () {
    return this.y * 10;
}
var bash = new ConstructFoo();//new keyword is syntactic sugar for creating a new instance
alert(bash.y);//will alert 10
alert(bash.x);//ERROR, this is undefined
alert(bash.barMethod());//will alert 100
var baz = new ConstructFoo(); //new instance, lets prove it
alert(bash === baz); //will alert false
bash = "";
alert(bash.y);//ERROR, this is undefined
alert(baz.y);//alerts 10
不要与JavaScript对象混淆:

var foo = {
    funcA: function (params) {
       var bar = 2;
       //etc
       return bar;
    },
    funcB: function (param) {
        var bar = param * 2;
        //etc
        return bar;
    }
};

foo.funcA(7);//returns 2
foo.funcB(2); //returns 4
这是一个静态对象。您不会创建它的新实例。如果在其他地方更改foo.funcA,它将影响使用该函数的所有代码


建议阅读:O'Reilly的JavaScript模式

你所说的
does item..1
是什么意思?编辑了这个问题。希望这能解释…我还是不知道你的意思,对不起。不执行
Does item..1..for eg..var result=arg1*2只是指一些操作?您可以发布代码示例的Python等价物吗?是的,只是一些操作……一种方法是传入一个参数,告诉您的代码要执行哪个部分。例如func1(arg1,action){if(action==“1”){…}否则{…}我不想func2调用func1内部的另一个函数…我希望func1对func2的调用有不同的行为。在我的例子中,只需执行相同函数和break的较少代码行,并返回值。完全正确!这就是目标。请记住,
参数。被调用方在JavaScript严格模式下是不允许的。@Jack\u of_All\u Trades替代方案就是不启用严格模式(这是您通过在文件或函数顶部添加
“use strict”
手动启用的)。
var ConstructFoo = function () { //capitalise constructor names
    var x = "my private variable";//remember, JS has FUNCTIONAL scope
    this.y = 10; //we can read and write to this outside of this function
}
ConstructFoo.prototype.barMethod = function () {
    return this.y * 10;
}
var bash = new ConstructFoo();//new keyword is syntactic sugar for creating a new instance
alert(bash.y);//will alert 10
alert(bash.x);//ERROR, this is undefined
alert(bash.barMethod());//will alert 100
var baz = new ConstructFoo(); //new instance, lets prove it
alert(bash === baz); //will alert false
bash = "";
alert(bash.y);//ERROR, this is undefined
alert(baz.y);//alerts 10
var foo = {
    funcA: function (params) {
       var bar = 2;
       //etc
       return bar;
    },
    funcB: function (param) {
        var bar = param * 2;
        //etc
        return bar;
    }
};

foo.funcA(7);//returns 2
foo.funcB(2); //returns 4