在JavaScript中,反引用†似乎与单引号相同.例如,我可以使用反引号来定义这样的字符串:
var s = `abc`;
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使反引号的行为实际上与单引号的行为不同?
†请注意,在程序员中,"反引号"是更常被称为重音符号的名称.程序员有时也使用替代名称 "反引号"和"反向".此外,在Stack Overflow和其他地方,"反击"的其他常见拼写是"后退"和"后退".
javascript backticks template-strings single-quotes backquote
编辑2016年10月:请注意这个问题是在2012年提出的.每个月左右有人添加一个新的答案或评论反驳答案,但这样做没有意义,因为问题可能已经过时(记住,这是Gnome Javascript编写gnome-shell扩展,而不是浏览器的东西,这是非常具体的).
按照我之前关于如何在Javascript中进行子类化的问题,我正在创建一个超类的子类,如下所示:
function inherits(Child,Parent) {
var Tmp = function {};
Tmp.prototype = Parent.prototype;
Child.prototype = new Tmp();
Child.prototype.constructor = Child;
}
/* Define subclass */
function Subclass() {
Superclass.apply(this,arguments);
/* other initialisation */
}
/* Set up inheritance */
inherits(Subclass,Superclass);
/* Add other methods */
Subclass.prototype.method1 = function ... // and so on.
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何使用这种语法在原型上定义一个setter/getter?
我曾经做过:
Subclass.prototype = {
__proto__: Superclass.prototype,
/* other methods here ... */
get myProperty() {
// code. …Run Code Online (Sandbox Code Playgroud) 可能重复:
在读取之前设置未定义的javascript属性
在Javascript中是否有相当于Python的defaultdict?这将是一个Javascript数组,其中为缺少的键返回的值是可定义的.就像是:
var a = defaultArray("0");
console.log(a['dog']);
// would print 0
Run Code Online (Sandbox Code Playgroud)
如果没有,你会如何实现它?
嗨,有没有办法在javascript中重载'.'(点)和[]运算符.即如果我说obj.Name或obj ['Name']它应该通过传递Name作为参数来调用obj类中的公共方法.python中使用属性方法提供的类似功能.但是在这里我希望将".Name"作为参数传递给common方法.
像这样..
function Data(){
this.getValue(name){
return '...'
}
}
data = new Data()
name = data.Name
name = data['Name']
//both should call data.getValue()
Run Code Online (Sandbox Code Playgroud) 这可能还是我在这里咆哮错了?
var data = 'one';
function fnc(){
this.out = function(){
return data;
}
}
var instance = new fnc();
alert(instance.out);
data = 'two';
alert(instance.out);
// I know that this would achieve that, but that's not what I would like to know.
alert(instance.out());
data = 'two';
alert(instance.out());
Run Code Online (Sandbox Code Playgroud)
更新:
应该表示fnc的对象实际上是Sarissa dom文档.这是fnc(),dom_doc()的更精细版本.以下接受的答案已集成到下面的功能中.
function get_doc(dom_node) {
var doc;
if (navigator.userAgent.indexOf("MSIE") >= 0) {
doc = new ActiveXObject("Msxml2.DOMDocument.3.0");
doc.loadXML(document.getElementById(dom_node).text);
}
else {
doc = Sarissa.getDomDocument();
doc = (new DOMParser()).parseFromString(document.getElementById(dom_node).textContent, "text/xml");
// runs XSLTProcessor in modern …Run Code Online (Sandbox Code Playgroud) // A simple Javascript Object / String
var object = "hello";
// Imagine a button in the DOM, and if it's clicked the object value will change
document.getElementById("button").onclick = function(){
window.object = "hello world";
}
// Now I would like to track and do something when the object is changed, ex:
object.onreadystatechange = function(){
alert(object);
}
Run Code Online (Sandbox Code Playgroud)
注意:这听起来很愚蠢,因为我可以onclick在按钮上获得事件的变化,但这不是我想要的,我想严格跟踪对象本身的变化以供任何使用,上面的代码是只是一个例子.
我在JavaScript中有一个自定义对象:
function Graph (dataType, provider){
this.dataType = dataType;
this.provider = provider;
}
Run Code Online (Sandbox Code Playgroud)
我通过"new"实例化该对象,然后将其传递给我的代码的其他部分,这些部分可能需要跟踪对该对象属性的更改.
修改Graph对象的属性时,我希望通知该对象的所有其他使用者.在像C#这样的语言中,我会有一个setter,我可以在其中举起一个事件.在自定义JavaScript对象中创建事件的正确方法是什么?
谢谢.
我熟悉强类型的OOP语言,如C#和Java,所以我对Javascript有点困惑.我希望我class被封装,例如:
function Human() {
var _name = '';
var _nameChangeCounter = 0;
}
Human.constructor = Human;
Human.prototype = Object.create(Animal);
Run Code Online (Sandbox Code Playgroud)
如您所见,Human扩展了Animal类.现在我需要一个吸气剂和吸气剂Name,以及吸气剂NameChangeCounter.在二传手中Name,它应该增加NameChangeCounter.我在这个问题中查找了如何在Javascript中制作getter和setter :
Name.prototype = {
get fullName() {
return this.first + " " + this.last;
},
set fullName(name) {
var names = name.split(" ");
this.first = names[0];
this.last = names[1];
}
};
Run Code Online (Sandbox Code Playgroud)
但是,现在原型被用于继承,我该怎么办呢?我必须做的Java风格(创建getName,setName,getNameChangeCounter功能)?物业如何window.location.href实施?
有没有办法在JavaScript类上侦听属性调用
例如,当我这样的事情:
myForm = new Form();
myForm.name = 'Name';
Run Code Online (Sandbox Code Playgroud)
- >当我设置名称时,我不想只设置属性,但我也想更新我的Vuex商店.同样的事情,get我想从Vuex商店阅读.
我知道有像Proxy这样的东西,但为此我需要用Proxy对象包装我的Class.不确定我是否喜欢这个.
module.exports = new Proxy(new Form({}), {
get (receiver, name) {
console.log('getting property from Vuex Store');
}
});
Run Code Online (Sandbox Code Playgroud)
我需要的是这样的事情:
module.exports = class Form {
//this should be triggered when form.something
get(property) {
return this[property];
}
//this should be triggered when from.something = 'something'
set(property, value) {
return this[property] = value;
}
};
Run Code Online (Sandbox Code Playgroud)
它有一个最好的做法吗?
我试图了解以下优点是什么(如果有的话):
const obj = {
forename: 'Foo',
surname: 'Bar',
get name() {
//yes I get that I can do other stuff here
return this.forename+' '+this.surname;
}
}
alert(obj.name); //Foo Bar
Run Code Online (Sandbox Code Playgroud)
...超过...
const obj = {
forename: 'Foo',
surname: 'Bar',
name() {
return this.forename+' '+this.surname;
}
}
alert(obj.name()); //Foo Bar
Run Code Online (Sandbox Code Playgroud)
我已经阅读了 ( 1 ; 2 ; 3 ) 但似乎看不到除了可读性和代码风格之外的任何好处。仅此而已吗?两种方法之间没有隐含的行为变化?
是否只关注 JavaScript 中未来的类属性/方法可见性?这是有道理的 - 私有财产的吸气剂。但是因为那还没有,我看不出上面的意思。
任何人都可以启发我吗?
我在命名空间中有一个JavaScript数组,如下所示:
app.collec.box = [];
Run Code Online (Sandbox Code Playgroud)
我在同一个命名空间内有一个函数,如下所示:
app.init = function () {
var box = this.collec.box;
// ... code to modify box
};
Run Code Online (Sandbox Code Playgroud)
我认为设置一个局部变量等于一个对象或对象属性只是对原始的一个参考,但似乎我错了,在box我的函数内部更改局部变量的内容后,app.collec.box不会改变.
请帮忙,我做错了什么?我该怎么解决这个问题?
提前致谢.
编辑.这是完整的代码.
var app = {
collec: {
box: [],
cache: []
},
init: function () {
var box = this.collec.box;
$.ajax({
url: 'file.json',
success: function (json) {
// Map JSON array to box array using Underscore.js _()map
box = _(json).map(function (o) {
return new Model(o);
});
}
});
}
};
app.init();
Run Code Online (Sandbox Code Playgroud) 我想出了下面的情况:
function Dog () {
"use strict";
this.age = 1;
var name = "Fido";
this.getName = function () { return name; }
}
Run Code Online (Sandbox Code Playgroud)
现在我正在创建一个"Dog"类的新实例并打印变量的值.
var d = new Dog;
document.write('<strong>Dog age:</strong> ' +d.age); \\Outputs "1" as expected
document.write('<br/>');
document.write('<strong>Dog name:</strong> ' +d.name); \\Outputs "undefined" as expected, 'cause it's a private variable.
document.write('<br/>');
document.write('<strong>Get Dog name:</strong> ' +d.getName()); \\Outputs "Fido", as expected.
Run Code Online (Sandbox Code Playgroud)
但是,假设我想改变Dog的名字,如下所示:
d.name = "Stinky";
document.write('<br/>');
document.write('<strong>Dog name Again:</strong> ' +d.name);
document.write('<br/>');
document.write('<strong>Get Dog name Again:</strong> ' +d.getName());
Run Code Online (Sandbox Code Playgroud)
基于此,我提出了几个问题:
在下面的代码中,我定义了Greeter.prototype.isVeryPolite = function(){...用于访问this._isVeryPolite
greeter.isVeryPolite()
Run Code Online (Sandbox Code Playgroud)
但最后的"()"不是非常用户友好.有没有javascript的技巧,能够拥有greeter.isVeryPolite而无需直接访问会员?
https://jsfiddle.net/5r4so2Ld/
var Greeter = (function () {
function Greeter(message, flag) {
this._name = message;
this._isVeryPolite = flag;
}
Greeter.prototype.greet = function () {
if (this._isVeryPolite) {
return "How do you do, " + this._name;
}
else {
return "Hello " + this._name;
}
};
Greeter.prototype.isVeryPolite = function () {
return this._isVeryPolite;
};
return Greeter;
})();
var greeter = new Greeter("world", true);
var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
alert(greeter.greet()); …Run Code Online (Sandbox Code Playgroud) javascript ×13
oop ×3
getter ×2
backquote ×1
backticks ×1
ecmascript-5 ×1
ecmascript-6 ×1
function ×1
object ×1
private ×1
python ×1
string ×1
variables ×1