gpt4 book ai didi

javascript - 在 Javascript 中创建库的正确方法

转载 作者:行者123 更新时间:2023-12-03 07:04:54 24 4
gpt4 key购买 nike

有人可以告诉我从以下选项创建库的正确方法吗?

     //option 1

function Fruit(){
var color,shape;
this.start = function(l,callback){
color = "Red"; shape = "Circle";
return callback();
}
}

//option2

function Fruit(){

this.start = function(l,callback){
this.color = "Red"; this.shape = "Circle";
return callback();
}
}

//option3

var Fruit = {
var color,shape;
start : function (l,callback) {
color = "Red"; shape = "Circle";
return callback();
}
}

我想知道哪种方法是在其中创建对象和函数的正确方法。如果这三个选项都是错误的,谁能告诉我正确的方法。

最佳答案

不过,就我个人喜好而言,剥猫皮的方法有很多种。请随意更改变量名称等...

//option 4

function Fruit(_fruit, _callback){
var that = this;
this.color = '';
this.shape = '';

var init = function(f, c){
switch(f){
case 'apple':
that.color = 'red';
that.shape = 'circle'
break;
}
return c();
}

init(_fruit, _callback);
}

var apple = new Fruit('apple', function(){
// Although you don't really need a callback as you're not doing any async tasks...
alert('Apple generated');
});

关于javascript - 在 Javascript 中创建库的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36869579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com