我们如何获得在Javascript中引发异常的方法?
例如:
<StackPanel Name="spItemDisplay" HorizontalAlignment="Stretch" Margin="0,10,0,0" Grid.Row="3" VerticalAlignment="Stretch" Orientation="Horizontal" >
<ScrollViewer HorizontalAlignment="Right" >
<DataGrid HorizontalAlignment="Center" Width="1096" Name="dgItemDisplay" AutoGenerateColumns="False" Height="auto" >
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" x:Name="dgItemCode" Width="300" Header="Barcode" Binding="{Binding ItemCode}" />
<DataGridTextColumn IsReadOnly="True" x:Name="dgItemName" Width="500" Header="Item Name" Binding="{Binding ItemName}" />
<DataGridTextColumn IsReadOnly="True" x:Name="dgItemPrice" Width="250" Header="Item Price" Binding="{Binding ItemPrice, StringFormat=RM {0}}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate >
<DataTemplate>
<StackPanel Name="spItemDisplay2" HorizontalAlignment="Stretch" Background="White" >
<Grid Margin="0,10" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="300" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<TextBlock Text="Quantity: " FontWeight="Bold" Grid.Column="2" Grid.Row="0"/>
<TextBox KeyDown="TxtQty_KeyDown" x:Name="txtQty" Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="0"/>
<TextBlock Text="Discount: " FontWeight="Bold" Grid.Column="2" Grid.Row="1"/>
<TextBox KeyDown="TxtDisc_KeyDown" x:Name="txtDisc" Text="{Binding Discount, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="1"/>
<Button Content="Remove Item" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="95" Click="btnRemoveItem_Click" Grid.Column="3" FontSize="10" Grid.Row="3"/>
</Grid>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</ScrollViewer>
</StackPanel>
尝试:
function method()
try { throw new Error('oh oh')}
catch(e) { e.stack... how to get the method ...}
但是它显示为 console.log("The method is: " + e.method);
。
答案 0 :(得分:2)
.stack
属性将包含显示堆栈跟踪的字符串-第一行将引发错误,第二行将列出包含函数名称(如果有)以及引发错误的行/列号:
function method() {
try {
throw new Error('oh oh')
} catch (e) {
console.dir(e.stack.split('\n')[1]);
}
}
method();
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以使用arguments.callee.name
function method() {
try {
throw new Error('oh oh')
} catch (e) {
console.log(arguments.callee.name)
}
}
method();