gpt4 book ai didi

javascript - JS中这两种编码风格有什么区别?

转载 作者:行者123 更新时间:2023-11-29 10:18:13 24 4
gpt4 key购买 nike

<分区>

选项 1:

<script>
var myObject = function(){
this.iAm = 'an object';
this.whatAmI = function(){
alert('I am ' + this.iAm);
};
}
var myNewObject = new myObject();
myNewObject.whatAmI();
</script>

选项 2:

<script>
function myObject(){
this.iAm = 'an object';
this.whatAmI = function(){
alert('I am ' + this.iAm);
};
};
var myNewObject = new myObject();
myNewObject.whatAmI();
</script>

问题:

这两个代码都可以工作。但是它们之间有什么区别呢?在 js 中进行 OOP 编码时,哪种方法更好?

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