程序设计

专业和发烧友程序员的问答

8
哪种方法最适合在JavaScript中创建对象?对象属性之前是否必须使用“ var”?
到目前为止,我看到了用JavaScript创建对象的三种方法。哪种方法最适合创建对象,为什么? 我还看到在所有这些示例中,var在属性之前未使用关键字-为什么?var提到属性是变量时,是否不必在属性名称之前声明? 在第二和第三种方式中,对象的名称为大写,而在第一种方式中,对象的名称为小写。我们应该在什么情况下使用对象名称? 第一种方式: function person(fname, lname, age, eyecolor){ this.firstname = fname; this.lastname = lname; this.age = age; this.eyecolor = eyecolor; } myFather = new person("John", "Doe", 50, "blue"); document.write(myFather.firstname + " is " + myFather.age + " years old."); 第二种方式: var Robot = { metal: "Titanium", killAllHumans: function(){ alert("Exterminate!"); …
177 javascript  object 


30
为什么省略花括号被认为是不好的做法?[关闭]
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案会得到事实,参考或专业知识的支持,但是这个问题可能会引起辩论,争论,民意调查或扩展讨论。如果您认为此问题可以解决并且可以重新提出,请访问帮助中心寻求指导。 8年前关闭。 为什么每个人都告诉我编写这样的代码是一种不好的做法? if (foo) Bar(); //or for(int i = 0 i < count; i++) Bar(i); 关于省略花括号的最大论据是,有时花括号可能是花括号的两倍。例如,以下代码为C#中的标签绘制发光效果。 using (Brush br = new SolidBrush(Color.FromArgb(15, GlowColor))) { for (int x = 0; x <= GlowAmount; x++) { for (int y = 0; y <= GlowAmount; y++) { g.DrawString(Text, this.Font, br, new Point(IconOffset …
177 java  c#  c++  c  coding-style 

19
如何将UIImage旋转90度?
我有一个UIImage是UIImageOrientationUp(纵向),我想逆时针旋转90度(景观)。我不想使用CGAffineTransform。我希望的像素UIImage实际移动位置。我正在使用一个代码块(如下所示),该代码块最初旨在调整a的大小UIImage以实现此目的。我将目标尺寸设置为的当前尺寸,UIImage但出现错误: (错误):CGBitmapContextCreate:无效数据字节/行:对于8个整数位/分量,3个分量kCGImageAlphaPremultipliedLast,应至少为1708。 (只要提供SMALLER大小作为目标大小BTW,我都不会出错)。如何UIImage在保留当前尺寸的同时仅使用核心图形功能旋转90度逆时针旋转? -(UIImage*)reverseImageByScalingToSize:(CGSize)targetSize:(UIImage*)anImage { UIImage* sourceImage = anImage; CGFloat targetWidth = targetSize.height; CGFloat targetHeight = targetSize.width; CGImageRef imageRef = [sourceImage CGImage]; CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); if (bitmapInfo == kCGImageAlphaNone) { bitmapInfo = kCGImageAlphaNoneSkipLast; } CGContextRef bitmap; if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { …

18
安装失败并显示消息无效文件
安装失败并显示消息无效文件:K:\ project \ app \ build \ intermediates \ split-apk \ with_ImageProcessor \ debug \ slices \ slice_0.apk。可以通过卸载apk的现有版本(如果存在)然后重新安装来解决此问题。 警告:卸载将删除应用程序数据! 您要卸载现有的应用程序吗? 我正在android studio 2.3 beta 3中运行我的项目。

14
按钮背景为透明
我有一个按钮。当我按下按钮时,我必须将文本设置为粗体,否则会正常显示。因此,我为粗体和普通写样式。 <style name="textbold" parent="@android:style/TextAppearance"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textStyle">bold</item> </style> <style name="textregular" parent="@android:style/TextAppearance"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textStyle">normal</item> </style> 现在我有一个button_states.xml为: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="true" style="@style/textbold" /> <item android:state_focused="false" android:state_pressed="true" style="@style/textregular" /> <item style="@style/textregular" /> </selector> 在此按钮的布局中,我也必须使背景也透明...我将如何做?我的布局代码是: <Button android:id="@+id/Btn" android:background="@drawable/button_states" /> 如何在样式中加入透明背景?
177 android 

11
将Unicode转换为ASCII且在Python中没有错误
我的代码只是抓取一个网页,然后将其转换为Unicode。 html = urllib.urlopen(link).read() html.encode("utf8","ignore") self.response.out.write(html) 但是我得到了UnicodeDecodeError: Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 507, in __call__ handler.get(*groups) File "/Users/greg/clounce/main.py", line 55, in get html.encode("utf8","ignore") UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 2818: ordinal not in range(128) 我认为这意味着HTML在某处包含一些错误的Unicode尝试。我可以删除导致问题的任何代码字节而不出错吗?


13
在OSX中卸载所有已安装的gem?
在某些情况下,我想还原并卸载所有以前的gem安装。 例如,我需要帮助一个朋友迁移他们的Rails开发机器以使用RVM。由于他们以前在整个系统中使用过gem,所以在处理多个项目时,他遇到了许多麻烦。 本质上,他是RVM转换的后代。 如何优雅地卸载他的OSX系统上的所有gem?
177 ruby  rubygems 




18
检查字符串是否仅包含数字的最快方法
我知道几种检查方法。正则表达式,int.parse,tryparse,循环。 谁能告诉我最快的检查方法是什么? 需要的只是检查,不需要实际解析。 这与以下问题不同:如何确定字符串是否为数字? 问题不仅在于如何识别。但是关于什么是最快的方法。
177 c# 

5
如何在代码中触发jQuery change事件
我有一个运行正常的更改事件,但需要递归进行。 因此,我有一个在更改时触发的函数,该函数将基于类选择器“更改”其他下拉菜单(注意“ drop downS”,可能有多个)。此代理更改不会触发功能,因此会失败。我如何使它工作? 码 $(document).ready(function () { var activeDropBox = null; $("select.drop-box").change(function () { var questionId = $(this).attr("questionId"); var selectedAnswer = $(this).val(); activeDropBox = this; alert(this.questionId); $.ajax( { type: "POST", url: answerChangedActionUrl, data: { questionId: questionId, selectedValue: selectedAnswer }, success: function (data) { SetElementVisibility(data.ShowElement, questionId); }, error: function (XMLHttpRequest, …

18
Swift:声明一个空字典
我开始swift关注The Swift Programming LanguageApple提供的iBook- on Swift。这本书说要创建一个空字典,应该使用[:]与声明数组相同的方式[]: 我声明了一个空数组,如下所示: let emptyArr = [] // or String[]() 但是在声明空字典时,出现语法错误: let emptyDict = [:] 如何声明空字典?
177 ios  swift  dictionary 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.