Javascript OOP - 继承和原型设计(Javascript OOP - inheritance and prototyping)
我试图在Javascript中使用OOP继承和原型设计。 请你看看我的JSfiddel http://jsfiddle.net/Charissima/daaUK/ 。 最后一个值是问题,谢谢。 我无法理解为什么带有raceCar的函数驱动器没有得到totalDistance,每个putTotalDistance设置一个。
function Car () { var that = this; this.totalDistance = 0; this.putTotalDistance = function(distance) { that.totalDistance = distance; }; this.getTotalDistance = function() { return this.totalDistance; }; this.drive = function(distance) { that.totalDistance += distance; return that.totalDistance; }; this.privateFunc = function() { return 'car ' + this.totalDistance; }; }; function RaceCar (initialDistance) { var that = this; this.prototype = new Car(); this.drive = function(distance) { return that.prototype.drive(2*distance); }; this.privateFunc = function() { return 'raceCar ' + that.getTotalDistance(); }; }; RaceCar.prototype = new Car(); car = new Car; raceCar = new RaceCar; car.putTotalDistance(200); alert('car totalDistance = ' + car.drive(10) + ' - ok'); raceCar.putTotalDistance(200); alert('raceCar totalDistance before drive = ' + raceCar.getTotalDistance() + ' - ok'); alert('raceCar totalDistance after drive = ' + raceCar.drive(10) + ' Why not 220?');
I'm trying to use OOP in Javascript with inheritance and prototyping. Would you please have a look at my JSfiddel http://jsfiddle.net/Charissima/daaUK/. The last value is the problem, thank you. I cannot understand why the function drive with raceCar doesn't get the totalDistance, which a set per putTotalDistance.
function Car () { var that = this; this.totalDistance = 0; this.putTotalDistance = function(distance) { that.totalDistance = distance; }; this.getTotalDistance = function() { return this.totalDistance; }; this.drive = function(distance) { that.totalDistance += distance; return that.totalDistance; }; this.privateFunc = function() { return 'car ' + this.totalDistance; }; }; function RaceCar (initialDistance) { var that = this; this.prototype = new Car(); this.drive = function(distance) { return that.prototype.drive(2*distance); }; this.privateFunc = function() { return 'raceCar ' + that.getTotalDistance(); }; }; RaceCar.prototype = new Car(); car = new Car; raceCar = new RaceCar; car.putTotalDistance(200); alert('car totalDistance = ' + car.drive(10) + ' - ok'); raceCar.putTotalDistance(200); alert('raceCar totalDistance before drive = ' + raceCar.getTotalDistance() + ' - ok'); alert('raceCar totalDistance after drive = ' + raceCar.drive(10) + ' Why not 220?');
原文:https://stackoverflow.com/questions/19431042
最满意答案
为您的图像视图创建一个
@IBOutlet
。然后,假设您的图像保存在Images.xcassets中:
@IBAction func option1(sender:AnyObject){ var randomNumber = Int(arc4random_uniform(5)) yourImageViewIBOutlet.image = UIImage(named: options[randomNumber]) }
Make an
@IBOutlet
for your image view.Then, assuming your images are saved in Images.xcassets:
@IBAction func option1(sender:AnyObject){ var randomNumber = Int(arc4random_uniform(5)) yourImageViewIBOutlet.image = UIImage(named: options[randomNumber]) }
相关问答
更多-
这条线永远不会工作: animation.values = animationImages 将其更改为: animation.values = animationImages.map {$0.CGImage as AnyObject} 原因是你试图为这个图层的"contents"键设置动画。 但那是contents属性。 但contents属性必须设置为CGImage,而不是UIImage。 相比之下,您的animationImages包含UIImages,而不是CGImages。 因此,您需要将您的U ...
-
为什么我的图像没有像我指定的那样显示图像 - 在Swift中(Why is my Image not displaying images as I've specified - In Swift)[2023-09-23]
看起来你没有设置你的imageView.clipsToBounds = true 或者从属性检查器设置它 - >检查剪辑子视图 (图像的底部:]) Looks like you didn't set your imageView.clipsToBounds = true Or set it from attributes inspector -> check Clip Subviews (Bottom of the image :] ) -
在Swift中移动图像(Moving Images In Swift)[2022-10-25]
查看关于UIView动画的Ray Wenderlich教程 。 Check out this Ray Wenderlich Tutorial on UIView Animations. -
在每个案例之后放入break语句并尝试。 并删除半冒号。 I just realized the problem was that I copied the code from the view controller to the button class, and didn't change target: self to target: viewController. But thanks for all the answers anyways...
-
为您的图像视图创建一个@IBOutlet 。 然后,假设您的图像保存在Images.xcassets中: @IBAction func option1(sender:AnyObject){ var randomNumber = Int(arc4random_uniform(5)) yourImageViewIBOutlet.image = UIImage(named: options[randomNumber]) } Make an @IBOutlet for your image vie ...
-
首先,它取决于您的图像大小: var image: NSData = card.photo as NSData cell.logo.image = UIImage(data: image) 这条线可能很重。 您可以像这样轻松地测量它,例如: let time1 = CACurrentMediaTime() var image: NSData = card.photo as NSData cell.logo.image = UIImage(data: image) print(CACurrentMediaT ...
-
这是因为你在ImageView里面设置了didFinishPickingImage图像,解决了需要维护一些状态的问题,比如单击哪个Button来设置Image ,为此你可以创建一个Bool实例并在Button的动作中分配它的值喜欢这个。 var isFromFirst: Bool = false @IBAction func showActionSheet1(sender: AnyObject) { self.isFromFirst = true ...//your other code ...
-
从UIActivity activityImage的文档: 图像的alpha通道用作遮罩,以生成呈现给用户的最终图像。 图像本身中的任何颜色数据都将被忽略。 不透明像素具有应用于它们的渐变,然后将此渐变放置在标准背景之上。 因此,完全不透明的图像将产生渐变填充矩形。 本文档中缺少的是,仅当activityCategory设置为UIActivityCategoryAction ( .action )时才适用。 它曾经也适用于UIActivityCategoryShared ( .shared ),直到它在早期 ...
-
只需将此代码添加到您的UITableViewController 。 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { //In here do whatever you want to do when the user selects a row } Just add this code to your UITableViewCo ...
-
快速的图像(Images on swift)[2023-01-22]
如果您只想将图像从任何格式转换为.bmp ,您可以使用Automator,无需编程。 但是如果你想在Swift中这样做: import Cocoa let image = NSImage(byReferencingFile: "/path/to/image.jpg")! let bitmapRep = image.representations[0] as! NSBitmapImageRep let data = bitmapRep.representationUsingType(.NSBMPFileTy ...