| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Using_classes | [Back] [Original] |
Get to know MDN better
JavaScript JavaScript JavaScript new
JavaScript
const bigDay = new Date(2019, 6, 19);
console.log(bigDay.toLocaleDateString());
if (bigDay.getTime() < Date.now()) {
console.log("");
}
Date bigDay bigDay toLocaleDateString() getTime() Date Date.now()
Date JavaScript
class MyClass {
//
}
class MyClass {
//
constructor() {
//
}
//
myField = "foo";
//
myMethod() {
// myMethod
}
//
static myStaticField = "bar";
//
static myStaticMethod() {
// myStaticMethod
}
//
static {
//
}
//
#myPrivateField = "bar";
}
ES6
function MyClass() {
this.myField = "foo";
//
}
MyClass.myStaticField = "bar";
MyClass.myStaticMethod = function () {
// myStaticMethod
};
MyClass.prototype.myMethod = function () {
// myMethod
};
(function () {
//
})();
const myInstance = new MyClass();
console.log(myInstance.myField); // 'foo'
myInstance.myMethod();
new new new
const myInstance = MyClass(); // TypeError: Class constructor MyClass cannot be invoked without 'new'
new MyClass(); // ReferenceError: Cannot access 'MyClass' before initialization
class MyClass {}
const MyClass = class {
//
};
const MyClass = class MyClassLongerName {
// MyClass MyClassLongerName
};
new MyClassLongerName(); // ReferenceError: MyClassLongerName is not defined
Color RGB
class Color {
constructor(r, g, b) {
// RGB `this`
this.values = [r, g, b];
}
}
const red = new Color(255, 0, 0);
console.log(red);
Object { values: (3) [] }
values: Array(3) [ 255, 0, 0 ]
Color values RGB
function createColor(r, g, b) {
return {
values: [r, g, b],
};
}
class Color {
constructor(...values) {
this.values = values;
}
}
const red = new Color(255, 0, 0);
//
new
const red = new Color(255, 0, 0);
const anotherRed = new Color(255, 0, 0);
console.log(red === anotherRed); // false
this
class MyClass {
constructor() {
this.myField = "foo";
return {};
}
}
console.log(new MyClass().myField); // undefined
createX
Date setX setFullYear
Color
class Color {
constructor(r, g, b) {
this.values = [r, g, b];
}
getRed() {
return this.values[0];
}
}
const red = new Color(255, 0, 0);
console.log(red.getRed()); // 255
class Color {
constructor(r, g, b) {
this.values = [r, g, b];
this.getRed = function () {
return this.values[0];
};
}
}
Color
console.log(new Color().getRed === new Color().getRed); // false
setRed
class Color {
constructor(r, g, b) {
this.values = [r, g, b];
}
getRed() {
return this.values[0];
}
setRed(value) {
this.values[0] = value;
}
}
const red = new Color(255, 0, 0);
red.setRed(0);
console.log(red.getRed()); // 0
getRed setRed values
class Color {
constructor(r, g, b) {
this.values = [r, g, b];
}
}
const red = new Color(255, 0, 0);
red.values[0] = 0;
console.log(red.values[0]); // 0
HSL RGB
class Color {
constructor(r, g, b) {
// values HSL
this.values = rgbToHSL([r, g, b]);
}
getRed() {
return this.values[0];
}
setRed(value) {
this.values[0] = value;
}
}
const red = new Color(255, 0, 0);
console.log(red.values[0]); // 0 255 HSL H 0
#
class Color {
// Color #values
#values;
constructor(r, g, b) {
this.#values = [r, g, b];
}
getRed() {
return this.#values[0];
}
setRed(value) {
this.#values[0] = value;
}
}
const red = new Color(255, 0, 0);
console.log(red.getRed()); // 255
#privateField
console.log(red.#values); // SyntaxError: Private field '#values' must be declared in an enclosing class
Chrome JavaScript
JavaScript
values getRed setRed setRed R
class Color {
#values;
constructor(r, g, b) {
this.#values = [r, g, b];
}
getRed() {
return this.#values[0];
}
setRed(value) {
if (value < 0 || value > 255) {
throw new RangeError(" R ");
}
this.#values[0] = value;
}
}
const red = new Color(255, 0, 0);
red.setRed(1000); // RangeError R
values values[0] API
class Color {
#values;
constructor(r, g, b) {
this.#values = [r, g, b];
}
redDifference(anotherColor) {
// #values this
//
return this.#values[0] - anotherColor.#values[0];
}
}
const red = new Color(255, 0, 0);
const crimson = new Color(220, 20, 60);
red.redDifference(crimson); // 35
anotherColor Color #values undefined try/catch in
class Color {
#values;
constructor(r, g, b) {
this.#values = [r, g, b];
}
redDifference(anotherColor) {
if (!(#values in anotherColor)) {
throw new TypeError(" Color ");
}
return this.#values[0] - anotherColor.#values[0];
}
}
# "#values" in anotherColor "#values"
class BadIdeas {
#firstName;
#firstName; //
#lastName;
constructor() {
delete this.#lastName; //
}
}
class Counter extends HTMLElement {
#xValue = 0;
constructor() {
super();
this.onclick = this.#clicked.bind(this);
}
get #x() {
return this.#xValue;
}
set #x(value) {
this.#xValue = value;
window.requestAnimationFrame(this.#render.bind(this));
}
#clicked() {
this.#x++;
}
#render() {
this.textContent = this.#x.toString();
}
connectedCallback() {
this.#render();
}
}
customElements.define("num-counter", Counter);
HTML Counter
color.getRed() color.setRed() Java JavaScript
class Color {
constructor(r, g, b) {
this.values = [r, g, b];
}
get red() {
return this.values[0];
}
set red(value) {
this.values[0] = value;
}
}
const red = new Color(255, 0, 0);
red.red = 0;
console.log(red.red); // 0
red get set
getter setter
class Color {
constructor(r, g, b) {
this.values = [r, g, b];
}
get red() {
return this.values[0];
}
}
const red = new Color(255, 0, 0);
red.red = 0;
console.log(red.red); // 255
red.red = 0 Cannot set property red of #<Color> which has only a getter
class MyClass {
luckyNumber = Math.random();
}
console.log(new MyClass().luckyNumber); // 0.5
console.log(new MyClass().luckyNumber); // 0.3
this
class MyClass {
constructor() {
this.luckyNumber = Math.random();
}
}
Date Date.now() Date DateNow()
parseInt() JavaScript Number.parseInt()
Color RGB
class Color {
static isValid(r, g, b) {
return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255;
}
}
Color.isValid(255, 0, 0); // true
Color.isValid(1000, 0, 0); // false
static console.log(new Color(0, 0, 0).isValid); // undefined
class MyClass {
static {
MyClass.myStaticProperty = "foo";
}
}
console.log(MyClass.myStaticProperty); // 'foo'
Color
class Color {
#values;
constructor(r, g, b, a = 1) {
this.#values = [r, g, b, a];
}
get alpha() {
return this.#values[3];
}
set alpha(value) {
if (value < 0 || value > 1) {
throw new RangeError("Alpha 0 1 ");
}
this.#values[3] = value;
}
}
alpha 1 alpha Color
JavaScript extends
class ColorWithAlpha extends Color {
#alpha;
constructor(r, g, b, a) {
super(r, g, b);
this.#alpha = a;
}
get alpha() {
return this.#alpha;
}
set alpha(value) {
if (value < 0 || value > 1) {
throw new RangeError("Alpha 0 1 ");
}
this.#alpha = value;
}
}
super(r, g, b) this super() JavaScript super() this this = new Color(r, g, b)super() super() thisJavaScript this
this #alpha getter/setter
Color get red() ColorWithAlpha red
const color = new ColorWithAlpha(255, 0, 0, 0.5);
console.log(color.red); // 255
Object toString() toString() [object Object]
console.log(red.toString()); // [object Object]
RGB
class Color {
#values;
//
toString() {
return this.#values.join(", ");
}
}
console.log(new Color(255, 0, 0).toString()); // '255, 0, 0'
super
class ColorWithAlpha extends Color {
#alpha;
//
toString() {
// toString()
return `${super.toString()}, ${this.#alpha}`;
}
}
console.log(new ColorWithAlpha(255, 0, 0, 0.5).toString()); // '255, 0, 0, 0.5'
extends
class ColorWithAlpha extends Color {
// ...
static isValid(r, g, b, a) {
// isValid()
return super.isValid(r, g, b) && a >= 0 && a <= 1;
}
}
console.log(ColorWithAlpha.isValid(255, 0, 0, -1)); // false
JavaScript
class ColorWithAlpha extends Color {
log() {
console.log(this.#values); // SyntaxError: Private field '#values' must be declared in an enclosing class
}
}
const color = new ColorWithAlpha(255, 0, 0, 0.5);
console.log(color instanceof Color); // true
console.log(color instanceof ColorWithAlpha); // true
Date
function incrementDay(date) {
return date.setDate(date.getDate() + 1);
}
const date = new Date(); // 2019-06-19
const newDay = incrementDay(date);
console.log(newDay); // 2019-06-20
//
console.log(date); // 2019-06-20
Java JavaScript
Color
function isRed(color) {
return color.red === 255;
}
function isValidColor(color) {
return (
color.red >= 0 &&
color.red <= 255 &&
color.green >= 0 &&
color.green <= 255 &&
color.blue >= 0 &&
color.blue <= 255
);
}
// ...
Color API
JavaScript
Map Set get()set()has() Date Unix Error TypeError ReferenceError Error instanceof JavaScript
| Web Proxy Viewer | New URL | Original Page |