简体   繁体   English

我不了解该程序的工作

[英]I don't understand working of the program

I am learning objects in JavaScript and I don't understand methods assigned as property to objects, when objects are defined through user-defined functions. 我正在用JavaScript学习对象,当通过用户定义的函数定义对象时,我不理解分配给对象的属性的方法。

This is a code snippet from tutorial point.com/JavaScript 这是来自point.com/JavaScript教程的代码片段

 <script type = "text/javascript"> function addPrice(amount) { with(this){ price = amount; } } function book(title, author) { this.title = title; this.author = author; this.price = 0; this.addPrice = addPrice; } </script> <script type = "text/javascript"> var myBook = new book("Perl", "Mohtashim"); myBook.addPrice(100); document.write("Book title is : " + myBook.title + "<br>"); document.write("Book author is : " + myBook.author + "<br>"); document.write("Book price is : " + myBook.price + "<br>"); </script> 

I want to know how the this.addPrice = addPrice is working and why if I remove the line no output is shown? 我想知道this.addPrice = addPrice的工作方式,为什么如果我删除该行,则不会显示任何输出?

JS is pretty flexible language. JS是非常灵活的语言。 In JS this: 在JS中:

 var obj = { name: "Raju Ritigya", sayHi: function() { console.log("hello, I'm " + this.name); } }; 

Is the same as this: 与此相同:

 var obj = {}; obj.name = "Raju Ritigya"; obj.sayHi = function() { console.log("hello, I'm " + this.name); }; 

Basically, there are two ways of adding properties and methods to an object in JS. 基本上,有两种向JS中的对象添加属性和方法的方法。

With that being said, your course is teaching you how to write "Classes", constructors and this in JS. 话虽这么说,您的课程正在教您如何在JS中编写“类”,构造函数以及this IMO if you're just starting with JS, this is too complex to understand. IMO,如果您只是从JS开始,这太复杂了,难以理解。 JS doesn't support Classes natively and it tries to mimic them with prototype inheritance. JS本身不支持类,而是尝试通过原型继承来模仿它们。

But anyway, here's my 0.02$ on what's going on there 但是无论如何,这是我的0.02美元

In JS you have primitive types ( string, number, boolean, symbol, null, undefined ) and everything else is an object ( yes, array is an object, function is an object, object is an object ). 在JS中,您具有原始类型( 字符串,数字,布尔值,符号,null,未定义 ),其他所有对象都是对象( 是,数组是对象,函数是对象,对象是对象 )。

Primitive values are passed around by value and they are immutable, but objects are passed by reference ( a point in memory ) and they are mutable 原始值按值传递,并且它们是不可变的,但是对象按引用传递( 内存中的一个点 ),并且它们是可变的

 var foo = {}; var bar = {}; console.log(foo === bar) //false 

Even though foo and bar look the same, they point to different places in memory, hence for JS they are not the same! 即使foobar看起来相同,它们也指向内存中的不同位置,因此对于JS,它们并不相同!

 var foo = {}; var bar = foo; bar.name = "random name"; console.log(foo.name); // "random name" 

And now foo and bar point to the same reference and making changes to one of them reflect to the other one. 现在foobar指向相同的引用,对其中一个进行更改会反映到另一个。

In JS every function needs to return something. 在JS中,每个函数都需要返回一些内容。 If you don't explicitly put a return statement in your function it will return undefined and if you use new keyword in front of your function call, it will return a new object that will have that function as a constructor. 如果未在函数中显式放置return语句,则它将返回undefined并且如果在函数调用之前使用new关键字,则它将返回一个具有该函数作为构造函数的新对象。

So, in conclusion, what's going on there is that you have a constructor ( book ) that's gonna return an object with 3 properties ( author, title, price ) and a method ( addPrice ). 因此,总而言之,这里发生的是您有一个构造函数( book ),该构造函数将返回具有3个属性( author,title,price )和一个方法( addPrice )的对象。 That method is a function (and as we already said, functions are objects in JS and can be easily passed around). 该方法是一个函数(正如我们已经说过的那样,函数是JS中的对象,可以轻松地传递)。 It would be exactly the same if you wrote your constructor like this: 如果您这样编写构造函数,则将完全相同:

 function book(title, author) { this.title = title; this.author = author; this.price = 0; this.addPrice = function(amount) { this.price = amount }; } 

As @deceze mentioned, using with is highly discouraged. 如@deceze所述,强烈建议不要使用with

By removing the this.addPrice = addPrice line, you don't add a method to your object but later on you try to call it on this line myBook.addPrice(100); 通过删除this.addPrice = addPrice行,您无需向对象添加方法,但是稍后您尝试在此行上调用myBook.addPrice(100);

Your code breaks on that line and JS won't continue executing the rest of your program (you can open console tab in dev tools and see the error there). 您的代码在该行中断,并且JS将不会继续执行程序的其余部分(您可以在dev tools打开console标签,然后在其中查看错误)。

Hope it helps, Cheers! 希望能有所帮助,干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM