如何使方法工作 - Javascript

时间:2016-09-27 09:57:26

标签: javascript arrays object methods

我想在我的对象中添加以下方法:

"你好 - 这个方法在控制台上记录字符串"你好,我的名字是"         其次是演员的名字。 "

有人可以帮我找到这个代码的错误吗? :

ButtonsModule

提前非常感谢你;)

4 个答案:

答案 0 :(得分:0)

您可能是初学者:)您正在将对象文字符号与构造函数混合使用。正确版本的构造函数在

之下
SimpleEmail mail = new SimpleEmail();
mail.setHostName(ConfigFactory.load().getString("mail.hostname"));
mail.setSmtpPort(ConfigFactory.load().getInt("mail.port"));
mail.setAuthenticator(new DefaultAuthenticator(ConfigFactory.load().getString("mail.username"), ConfigFactory.load().getString("mail.password")));
mail.setDebug(true);
mail.setMsg("Test");
mail.setTLS(true);
mail.setFrom("emailhere");
mail.addTo(email);
mail.setSubject("Activate your email");
mail.setMsg("message");
mail.send();

答案 1 :(得分:0)

if (tides_data.length() > 0) { for (int i = 0; i < tides_data.length(); i++) { String tide_type = ""; String tideTime = ""; String TimeOne = ""; String TimeAmPm = ""; String hrTime = " ", mnTime = ""; tideHeight_mtOne_string = ""; try { tide_values = tides_data.getJSONObject(i); tide_type = tide_values.optString("tide_type"); tideTime = tide_values.optString("tideTime"); tideHeight_mtOne_string = tide_values.optString("tideHeight_mt"); /*tideHeight_mtOne_string="5.58"; tide_type="HIGH"; tideTime="0:23 AM";*/ String timeArray[] = tideTime.split(" "); TimeOne = timeArray[0]; TimeAmPm = timeArray[1]; //---if pm then add 12 h on currect time if (TimeOne.contains(":")) { String timeHourMin[] = TimeOne.split(":"); hrTime = timeHourMin[0]; mnTime = timeHourMin[1]; int ckmin = Integer.parseInt(mnTime); if (TimeAmPm.equalsIgnoreCase("PM")) { hrTime = String.valueOf(Integer.parseInt(hrTime) + 12); //check min 31 so add 1 with hour for round figher if (ckmin > 31) { hrTime = String.valueOf(Integer.parseInt(hrTime) + 1); TimeOne = hrTime; } else { TimeOne = hrTime; } } else { //check min 31 so add 1 with hour for round figher if (ckmin > 31) { hrTime = String.valueOf(Integer.parseInt(hrTime) + 1); TimeOne = hrTime; } else { TimeOne = hrTime; } } } if (tide_type.equalsIgnoreCase(HIGH)) { if (TimeOne.equals("0")) { tideHeight_mtZero = Double.parseDouble(tideHeight_mtOne_string); Log.e("12345------>", "" + TimeOne); } if (TimeOne.equals("1")) { tideHeight_mtOne = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("2")) { tideHeight_mtTwo = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("3")) { tideHeight_mtThree = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("4")) { tideHeight_mtFour = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("5")) { tideHeight_mtFive = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("6")) { tideHeight_mtSix = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("7")) { tideHeight_mtSeven = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("8")) { tideHeight_mtEight = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("9")) { tideHeight_mtNine = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("10")) { tideHeight_mtTen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("11")) { tideHeight_mtEleven = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("12")) { tideHeight_mtTweelv = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("13")) { tideHeight_mtTherteen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("14")) { tideHeight_mtFourteen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("15")) { tideHeight_mtFiveteen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("16")) { tideHeight_mtSixteen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("17")) { tideHeight_mtSeventeen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("18")) { tideHeight_mtEighteen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("19")) { tideHeight_mtnineteen = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("20")) { tideHeight_mtTweenty = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("21")) { tideHeight_mtTweentyOne = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("22")) { tideHeight_mtTwentytwo = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("23")) { tideHeight_mtTwentythree = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("24")) { tideHeight_mtTwentyfour = Double.parseDouble(tideHeight_mtOne_string); } if (TimeOne.equals("25")) { tideHeight_mtTwentyfour = Double.parseDouble(tideHeight_mtOne_string); } } else { //-----only for hightide section } } catch (Exception e) { } } } CategorySeries seriesHighTide = new CategorySeries("Bar1"); CategorySeries mCurrentSeries = seriesHighTide; double[] y = {tideHeight_mtOne, tideHeight_mtTwo, tideHeight_mtThree, tideHeight_mtFour, tideHeight_mtFive, tideHeight_mtSix, tideHeight_mtSeven, tideHeight_mtEight, tideHeight_mtNine, tideHeight_mtTen, tideHeight_mtEleven, tideHeight_mtTweelv, tideHeight_mtTherteen, tideHeight_mtFourteen, tideHeight_mtFiveteen, tideHeight_mtSixteen, tideHeight_mtSeventeen, tideHeight_mtEighteen, tideHeight_mtnineteen, tideHeight_mtTweenty, tideHeight_mtTweentyOne, tideHeight_mtTwentytwo, tideHeight_mtTwentythree, tideHeight_mtTwentyfour, tideHeight_mtPm}; for (int i = 0; i < x.length; i++) { // Log.e("Test----"+i,"=="+y[i]); seriesHighTide.add(String.valueOf(i), y[i]); } dataSetHigh = new XYMultipleSeriesDataset(); // collection of series under one object.,there could any dataSetHigh.addSeries(seriesHighTide.toXYSeries()); XYSeriesRenderer renderer = new XYSeriesRenderer(); // one renderer for one series renderer.setColor(Color.GRAY); renderer.setDisplayChartValues(true); renderer.setChartValuesTextSize(20); renderer.setChartValuesSpacing((float) 5.5d); renderer.setLineWidth((float) 10.5d); mRendererForHighTide = new XYMultipleSeriesRenderer(); // collection multiple values for one renderer or series mRendererForHighTide.addSeriesRenderer(renderer); mRendererForHighTide.setShowLegend(false);//Removing below space in graph mRendererForHighTide.setZoomEnabled(false, false); mRendererForHighTide.setPanEnabled(false); mRendererForHighTide.setShowGridX(true); // this will show the grid in graph mRendererForHighTide.setShowGridY(true); mRendererForHighTide.setAntialiasing(true); mRendererForHighTide.setBarSpacing(.2); // adding spacing between the line or stacks mRendererForHighTide.setApplyBackgroundColor(true); mRendererForHighTide.setLabelsTextSize(10); mRendererForHighTide.setLegendTextSize(10); mRendererForHighTide.setApplyBackgroundColor(true); mRendererForHighTide.setBackgroundColor(Color.TRANSPARENT); mRendererForHighTide.setMarginsColor(getResources().getColor(R.color.transparent_background)); mRendererForHighTide.setBarWidth(10);//set bar with and hight mRendererForHighTide.setXAxisMin(0); mRendererForHighTide.setYAxisMin(0); mRendererForHighTide.setXAxisMax(25); mRendererForHighTide.setYAxisMax(11); mRendererForHighTide.addYTextLabel(-1, "-1mt"); mRendererForHighTide.addYTextLabel(-2, "-2mt"); mRendererForHighTide.addYTextLabel(-3, "-3mt"); mRendererForHighTide.addYTextLabel(-4, "-4mt"); mRendererForHighTide.addYTextLabel(-5, "-5mt"); mRendererForHighTide.addYTextLabel(-6, "-6mt"); mRendererForHighTide.addYTextLabel(-7, "-7mt"); mRendererForHighTide.addYTextLabel(-8, "-8mt"); mRendererForHighTide.addYTextLabel(-9, "-9mt"); mRendererForHighTide.addYTextLabel(-10, "-10mt"); mRendererForHighTide.addYTextLabel(0, "0mt"); mRendererForHighTide.addYTextLabel(1, "1mt"); mRendererForHighTide.addYTextLabel(2, "2mt"); mRendererForHighTide.addYTextLabel(3, "3mt"); mRendererForHighTide.addYTextLabel(4, "4mt"); mRendererForHighTide.addYTextLabel(5, "5mt"); mRendererForHighTide.addYTextLabel(6, "6mt"); mRendererForHighTide.addYTextLabel(7, "7mt"); mRendererForHighTide.addYTextLabel(8, "8mt"); mRendererForHighTide.addYTextLabel(9, "9mt"); mRendererForHighTide.addYTextLabel(10, "10mt"); mRendererForHighTide.setYLabelsPadding(15f);//spece between margin and yaxics line mRendererForHighTide.setYLabelsColor(0, Color.BLACK); mRendererForHighTide.setMargins(new int[]{30, 30, 30, 30}); // mRenderer.setShowCustomTextGrid(true); mRendererForHighTide.setXLabels(0); /*mRenderer.addXTextLabel(-1, "AM");*/ mRendererForHighTide.addXTextLabel(0, "AM"); mRendererForHighTide.addXTextLabel(1, ""); mRendererForHighTide.addXTextLabel(2, "2"); mRendererForHighTide.addXTextLabel(3, ""); mRendererForHighTide.addXTextLabel(4, "4"); mRendererForHighTide.addXTextLabel(5, ""); mRendererForHighTide.addXTextLabel(6, "6"); mRendererForHighTide.addXTextLabel(7, ""); mRendererForHighTide.addXTextLabel(8, "8"); mRendererForHighTide.addXTextLabel(9, ""); mRendererForHighTide.addXTextLabel(10, "10"); mRendererForHighTide.addXTextLabel(11, ""); mRendererForHighTide.addXTextLabel(12, "12"); mRendererForHighTide.addXTextLabel(13, ""); mRendererForHighTide.addXTextLabel(14, "2"); mRendererForHighTide.addXTextLabel(15, ""); mRendererForHighTide.addXTextLabel(16, "4"); mRendererForHighTide.addXTextLabel(17, ""); mRendererForHighTide.addXTextLabel(18, "6"); mRendererForHighTide.addXTextLabel(19, ""); mRendererForHighTide.addXTextLabel(20, "8"); mRendererForHighTide.addXTextLabel(21, ""); mRendererForHighTide.addXTextLabel(22, "10"); mRendererForHighTide.addXTextLabel(23, ""); mRendererForHighTide.addXTextLabel(24, "12"); mRendererForHighTide.addXTextLabel(25, "PM"); mRendererForHighTide.setXLabelsColor(Color.BLACK); chartHighTide.removeAllViews(); chart3 = ChartFactory.getBarChartView(TideActivityDemo.this, dataSetHigh, mRendererForHighTide, BarChart.Type.DEFAULT); chartHighTide.addView(chart3); 更改为

hello

循环播放演员并调用this.hello=function(){ //Rest of code } 方法

hello

JSFIDDLE

答案 2 :(得分:0)

首先定义你的函数,如

function sayHello (firstName, lastName, age, numOscars) {
      console.log("Hello my name is ", firstName," ", lastName, "I am", age, "years old, and I have ", numOscars, " Oscar(s).");
}

像这样创建你的actor数组,你可以添加更多的对象。

var actors = [{firstName: "Leonardo", lastName: "DiCaprio", age: "42", numOscars: 1}, {firstName: "example", lastName: "something", age: "12", numOscars: 0}];

您可以单独调用您的函数,如:

var leonardo = actors[0]
sayHello(leonardo.firstName, leonardo.lastName, leonardo.age, leonardo.numOscars);

或使用forEach:

actors.forEach(function(actor) {
   sayHello(actor.firstName, actor.lastName, actor.age, actor.numOscars);
   console.log('\n');
}

答案 3 :(得分:0)

function Person(firstName, lastName, age, numOscars) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.numOscars = numOscars;
}

  // If hello function is added to Person functions prototype
  // then each time a new Person object is created, 
  // hello function gets inherited rather than creating a new hello function
  Person.prototype.hello = function greeting() {
      // here firstName always refer to the specific object to which belongs
      console.log("Hello, my name is " + this.firstName);
  }

// instead of new Array()
// Use short hand form of Array declaration also called Array literal notation
var actors =[];


// create a new person object and add it to actors array
actors.push(new Person("Leonardo", "DiCaprio", 41, 1));
actors.push(new Person("Jennifer", "Lawrence", 25, 1));
actors.push(new Person("Samuel L."," Jackson", 67, false));
actors.push(new Person("Meryl", "Streep", 66, 3));
actors.push(new Person("John", "Cho", 43, false));

// loop through each of actors array members and call 
// hello function
actors.forEach(function(actor){
  actor.hello();
});