JavaScript工厂功能的正确语法是什么? JavaScript工厂功能语法混乱

发布于 2025-01-30 15:50:26 字数 928 浏览 3 评论 0原文

两者都函数getMyCar1& GetMyCar2具有相同的结果,但是哪一种是正确的方法?

getMyCar2:为什么值必须使用而不是键?钥匙:价值(Carbrand:Brand)。

function selectCar(brand, model, color){
    return{
        carBrand: brand,
        carModel: model,
        carColor: color, 

        getMyCar1: function(){
            return this.carBrand + " " + this.carModel + " " + this.carColor;  
         //It is said that no need this key word with factory function 
         //but without this key word not working, why?

        },

        getMyCar2: function(){ 
           return brand + " " + model + " " + color;  
           //NotWorking: return carBrand + carModel + carColor
        }
    };
}

让BMW = SelectCar(“ BMW”,“ X6”,“ White”); console.log(“我的汽车模型是:” + bmw.getmycar1());

让audi = selectcar(“ audi”,“ a8”,“ red”); console.log(“我的汽车模型是:” + audi.getmycar2());

Both function getMyCar1 & getMyCar2 has same result but which one is the correct way of doing?

getMycar2: Why value have to use instead of key? key:value (carBrand:brand).

function selectCar(brand, model, color){
    return{
        carBrand: brand,
        carModel: model,
        carColor: color, 

        getMyCar1: function(){
            return this.carBrand + " " + this.carModel + " " + this.carColor;  
         //It is said that no need this key word with factory function 
         //but without this key word not working, why?

        },

        getMyCar2: function(){ 
           return brand + " " + model + " " + color;  
           //NotWorking: return carBrand + carModel + carColor
        }
    };
}

let bmw = selectCar("bmw", "X6", "White");
console.log("My Car Model is: " + bmw.getMyCar1());

let audi = selectCar("Audi", "A8", "Red");
console.log("My Car Model is: " + audi.getMyCar2());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2025-02-06 15:50:26

这取决于您要实现的目标。 IE,
如果您希望该功能返回汽车实例的当前状态,则具有 getMyCar1 函数:
示例:

let bmw = selectCar("bmw", "X6", "White"); 
bmw.carColor= "black"
console.log("My Car Model is: " + bmw.getMyCar2());

结果是:

我的汽车模型是:宝马x6黑色

,我们更改了汽车颜色,并反映在功能中。

另一方面,如果您想要一种显示汽车初始状态的方法, getMyCar2 功能适合您,因为它确实会更改汽车属性。

let audi = selectCar("Audi", "A8", "Red");
audi.carColor = 'black'
console.log("My Car Model is: " + audi.getMyCar2());

我的汽车模型是:奥迪A8红色

getMyCar2 函数使用将函数传递给该函数的参数,因此当在汽车实例中更改任何属性时不会更改其输出

It depends on what you want to achieve. i.e.,
If you want that function to return the current state of a car instance, you have getMycar1 function:
Example:

let bmw = selectCar("bmw", "X6", "White"); 
bmw.carColor= "black"
console.log("My Car Model is: " + bmw.getMyCar2());

Result is:

My Car Model is: bmw X6 black

Here we changed car color and is reflected in the function.

on the other hand if you want a method to show initial state of car, getMyCar2 function is for you as it does change car attributes.

let audi = selectCar("Audi", "A8", "Red");
audi.carColor = 'black'
console.log("My Car Model is: " + audi.getMyCar2());

My Car Model is: Audi A8 Red

getMyCar2 function uses parameters passed to the function as values and so doesnt change its output when any attribute is changed in a car instance

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文