I am trying to create objects in Google Apps Script and I can't handle their properties. In the code below I don't understand why my build function don't work then the manualy sets neither
var my_object = Object.create(null,{
type: {value:"abc", enumerable:true},
name: {value:"abc", enumerable:true},
build: {
value:function(my_type,my_name){
this.type = my_type;
this.name = my_name;
return this;
}
}
});
var my_type = "my type";
var my_new_object = Object.create(my_object).build(my_type, "my name");
// In my_new_object I found the "abc" from definition but not the values from my_range and "my name"
console.log(my_type);
console.log(my_new_object.type);
console.log(my_new_object.name);
// And I don't succeed to set directly the properties neither
my_new_object.type = "my type";
my_new_object.name = "my name";
console.log(my_new_object.type);
console.log(my_new_object.name);
// I always get the "abc" from object definition
Do I need special setters and getters to handle object properties?
Thanks!
Add the writable properties to your object
type: {value:"abc", enumerable:true, writable: true},
name: {value:"abc", enumerable:true, writable: true},
I'm just curious, why not use the simplier approach?
function MyObject(type,name) {
this.type = type;
this.name = name;
}
var my_object = new MyObject("my_type","my_name");
Related
I am testing the Multilingual Bot downloaded from Microsoft bot framework. While doing so, some of my content are not getting translated.
Image link
You can see the following code where I have inserted few lines that asks the user if there is anything I can help ? This gets translated in to tthe language selected by the User. But, the content in CardAction() object Title 'Yes' and 'No' are not getting translated.
How to handle such translations in the middleware ?
bool translate = userLanguage != TranslationSettings.DefaultLanguage;
if (IsLanguageChangeRequested(turnContext.Activity.Text))
{
await _accessors.LanguagePreference.SetAsync(turnContext, turnContext.Activity.Text);
var reply = turnContext.Activity.CreateReply($"Your current language code is: {turnContext.Activity.Text}");
await turnContext.SendActivityAsync(reply, cancellationToken);
await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
// This content is getting partially translated.
var newRply = turnContext.Activity.CreateReply("Is there anything else I can help you with?");
newRply.SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
// The title is not getting translated
new CardAction() { Title = "Yes", Type = ActionTypes.PostBack, Value = Spanish },
// The title is not getting translated
new CardAction() { Title = "No", Type = ActionTypes.PostBack, Value = English },
},
};
await turnContext.SendActivityAsync(newRply);
}
else
{
var reply = turnContext.Activity.CreateReply("Choose your language:");
reply.SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
new CardAction() { Title = "EspaƱol", Type = ActionTypes.PostBack, Value = Spanish },
new CardAction() { Title = "English", Type = ActionTypes.PostBack, Value = English },
},
};
await turnContext.SendActivityAsync(reply);
}
}
Expecting that string in the CardAction() should also be translated into the language chosen by the user.
I assume you are using Microsoft Translator class that comes with the Sample. From the same sample, I implemented a new class (MultilingualCardAction) by inheriting CardAction class.
This works for me but there may be better ways as well.
public class MultilingualCardAction : CardAction
{
private readonly MicrosoftTranslator _translator;
private string _language;
public MultilingualCardAction(string language)
{
_language = language;
_translator = new MicrosoftTranslator(<<YOUR TRANSLATION KEY>>);
}
public string cardTitle
{
get
{
return this.Title;
}
set
{
this.Title = getTranslatedText(value).Result;
}
}
async Task<string> getTranslatedText(string title)
{
return await _translator.TranslateAsync(title, _language);
}
}
Then I created the CardAction object this way.
var newRply = turnContext.Activity.CreateReply("Is there anything else I can help you with?");
newRply.SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
new MultilingualCardAction('es') { cardTitle = "Yes", Type = ActionTypes.PostBack, Value = "Yes" },
new MultilingualCardAction('es') { cardTitle = "No, thanks!", Type = ActionTypes.PostBack, Value = "No" },
},
};
await turnContext.SendActivityAsync(newRply);
Refer the image below.
I am using the sample "SandwichOrder" code. When I use the property "Describe" to change the item value the bot doesn't understand the setted value.
public enum LengthOptions
{
[Describe("Test 1")]
SixInch = 1,
[Describe("Test 2")]
FootLong = 2
};
This is the output:
It's the problem how FormFlow handles the feedback after user's selection, the result is actually right the type of LengthOptions. Since we're not able to modify the source code of BotBuilder SDK, here is a workaround to solve this problem: we try to override the feedback of this item in FormFlow, and here is the code when building the FormDialog:
...
.Field(nameof(Length),
validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
var value = (LengthOptions)response;
result.Feedback = "Your selection means " + value;
return result;
})
...
The Length property in above code can be defined like this:
public enum LengthOptions
{
[Describe("Test 1")]
SixInch = 1,
[Describe("Test 2")]
FootLong = 2
};
public LengthOptions? Length { get; set; }
Here is the test result:
What #Grace Feng mentioned is one way to do that. Another simpler way would be to add the Terms decoration to LengthOptions each item.
So the code would be :
public enum LengthOptions
{
[Terms(new string[] { "Test 1" })]
[Describe("Test 1")]
SixInch = 1,
[Terms(new string[] { "Test 2" })]
[Describe("Test 2")]
FootLong = 2
};
Now your bot will automatically understand the value of "Test 1" as SixInch and "Test 2" as FootLong
i tried found answer in stackoverflow and can't find any answer about this
i just started learning NodeJS . and i got a question, is there any way how can i exports whole object with his functions or i can exports only functions of object?
thanks for advice!
when i try it i got error like this
TypeError: object is not a function
i got simple code like this :
animal.js
var Animal = {
name : null,
setName : function(name) {
this.name = name;
},
getName : function() {
console.log("name of animal is " + this.name);
}
}
exports.Animal = Animal;
and server.js
var animal = require('./animal');
var ani = new animal.Animal();
The error is because new expects a Function while Animal is a plain Object.
Though, with the Object, you could use Object.create() to create instances:
// ...
var ani = Object.create(animal.Animal);
Otherwise, you'll have to define Animal as a constructor Function:
function Animal() {
this.name = null;
// ...
}
exports.Animal = Animal;
Note: Depending on precisely what you want to accomplish, Functions are a type of Object and can hold additional properties.
function Animal(name) {
this.name = name || Animal.defaultName;
}
Animal.defaultName = null;
In AS2 I could do the following:
String.prototype.startsWith = function(s){
return this.indexOf(s) == 1
}
thus, startsWith is available on every String object
var s = "some string to test with";
s.startsWith("some") // returns true
and did it with great repository of cool tools:
var s = "some #VAR string";
s.startsWith("some");//returns true
s.endsWith("ing");//returns true
s.contains("#");//returns true
s.dataBind({VAR: "awsome"})// returns 'some awsome string'
s = "b";
s.isAnyOf("a","b","c"); //true, because "b" is one of the options
s.isInArr(["a","b","c"]); //true, because "b" is in the passed array
var o = { foo: function(i) { return "wind " + i } }
s = "foo";
f.call(o,3) //returns "wind 3" because method foo is ivoked on o with 3
f.apply(o,[3]) //returns "wind 3" because method foo is ivoked on o with 3
var a1 = [], a2 = []
s.push(a1,a2) // pushes s into a1 and a2
And so on, and so forth with many cool things that makes coding much more fun (and blazing fast when smartly used)
It's not just about String, I have such utils for Number, Date, Boolean, and so on.
Here's what I tried:
[Test]
public function test_stringPrototype()
{
String.prototype.startsWith = function(s):Boolean
{
return return this.indexOf(s) == 1;
}
assertTrue( !!String.prototype.startsWith ) //and so far - so good ! this line passes
var s:String = "some str";
assertTrue(!!o.startsWith ) //and this won't even compile... :(
}
And this won't even compile, not to mention pass or fail the test...
error: Access of possibly undefined property startsWith through a reference with static type String.
Whats the way to do it in AS3?
you could always have the utility class that will collect all those methods and work on the string, e.g.
package utils
{
public class StringUtils
{
public static function startsWith(input:String, test:String):Boolean
{
return input.indexOf(test) == 0;
}
}
}
usage:
trace(StringUtils.startWith("My string", "My"));
or as a "global" function:
package utils
{
public function startsWith(input:String, test:String):Boolean
{
return input.indexOf(test) == 0;
}
}
usage:
trace(startWith("My string", "My"));
best regards
Yes of course, use string representation of a variable name: "startsWith"; Example down
String.prototype.traceME = function():void
{
trace(this);
}
var s:String = "some str";
s["traceME"]();
else method:
var s:Object = new String("some str");
s.traceME();
I have a rootelement created from dynamic object. I want to get the object Id when I tapped an element:
var section = new Section("","Select a complex and then a property");
var root = new RootElement("Complexes"){section};
foreach(Complex complex in complexes){
var line = new RootElement(complex.Name);
foreach(Property property in complex.Properties){
line.Add(new Section(property.Name,"Select the property to work"){
new StringElement(property.Description, delegate {
// NEED HELP HERE!!! here I want to get property.Id
})
});
}
section.Add(line);
}
this.Root = root;
Any clue? Thanks a lot.
Copy property.Id in local variable and use in delegate
var line = new RootElement(complex.Name);
foreach(Property property in complex.Properties){
var propertyId = property.Id;
line.Add(new Section(property.Name,"Select the property to work"){
new StringElement(property.Description, delegate {
(new UIAlertView("title", "Id - " + propertyId, null, "Ok")).Show();
})
});
}
section.Add(line);