我有一个彼此相似的功能.如何在不重复的情况下更轻松地声明函数
function constructor (name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// create a Penguin constructor here
function Penguin(name, numLegs){
this.name=name;
this.numLegs = numLegs;
}
// create a sayName method for Penguins here
Penguin.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// our test code
var theCaptain = new Penguin("Captain Cook", …Run Code Online (Sandbox Code Playgroud) 我是Java的新手.
我似乎无法理解为什么这两个代码产生不同的输出.
请向我解释一下.
有什么区别y<=x;和y<=5;.正如你所看到的那样x是5,我不明白为什么我得到不同的输出.
for (int x = 0; x < 5; x++) {
for (int y = 1; y <=x ; y++) {
System.out.print("x");
}
for (int g = 4; g >= x; g--) {
System.out.print("*");
}
System.out.println();
}
Run Code Online (Sandbox Code Playgroud)
输出:
*****
x****
xx***
xxx**
xxxx*
Run Code Online (Sandbox Code Playgroud)
码:
for (int x = 0; x < 5; x++) {
for (int y = 1; y <= 5; y++) {
System.out.print("x");
}
for (int g = 4; …Run Code Online (Sandbox Code Playgroud)